2010-07-21 135 views
2

我目前有一个Palm WebOS应用程序,它使用Ajax.Request使用基本身份验证连接到Web服务。要发送用户名和密码,我只是简单地将它包含在URL(即http://username:[email protected]:port/)中,它工作得非常好,当密码包含除字母数字字符之外的任何内容时(例如,最近有一个用户发邮件给我, “和他的密码中的”&“,并且他无法连接,因为这些符号没有针对url进行正确解析)。有没有办法在url中发送凭据,以便我可以允许用户在密码中使用除字母数字之外的其他内容?通过Ajax.Request使用身份验证

var username = cookie.get().servers[this.serverIndex].username; 
    var password = cookie.get().servers[this.serverIndex].password; 
    this.auth = 'Basic ' + Base64.encode(username + ':' + password); 
    var baseUrl = 'http://' + url + ':' + port + '/gui/'; 
    this.baseUrl = baseUrl; 


    var request = new Ajax.Request(this.tokenUrl, { 
     method: 'get', 
     timeout: 2000, 
     headers: { 
      Authorization: this.auth 
     }, 
     onSuccess: successFunc, 
     onFailure: this.failure.bind(this) 
    }); 

反应是(我删除出于安全原因的网址):

{"request": {"options": {"method": "get", "asynchronous": true, "contentType": "application/x-www-form-urlencoded", "encoding": "UTF-8", "parameters": {}, "evalJSON": true, "evalJS": true, "timeout": 2000, "headers": {"Authorization": "Basic c3VubW9yZ3VzOmZyb2dneUAlMjY="}}, "transport": {"readyState": 4, "onloadstart": null, "withCredentials": false, "onerror": null, "onabort": null, "status": 401, "responseXML": null, "onload": null, "onprogress": null, "upload": {"onloadstart": null, "onabort": null, "onerror": null, "onload": null, "onprogress": null}, "statusText": "", "responseText": "", "UNSENT": 0, "OPENED": 1, "HEADERS_RECEIVED": 2, "LOADING": 3, "DONE": 4}, "url": "" 

回答

3

发送基本身份验证信息与标题: http://coderseye.com/2007/how-to-do-http-basic-auth-in-ajax.html

var auth = 'Basic ' + Base64.encode(user + ':' + pass); 
Ext.Ajax.request({ 
    url : url, 
    method : 'GET', 
    headers : { Authorization : auth } 
}); 
+0

好吧,试过了,我得到了一个401错误...用我的Ajax.Request调用和我的回复编辑了我的帖子... – 2010-07-21 12:40:25

+2

好吧,我明白了!我不得不将'header'改为'requestHeaders',并且一切都很好!谢谢! – 2010-07-21 13:06:23

0

你可以试着编码的用户名和密码,他们开始使用encodeURIComponent发送之前。

虽然更一般,你在IE8中测试过这种方法吗?因为它不再适用于普通请求 - 出于安全原因,username:[email protected]方案已被禁用。

虽然它可能是,但它们仍然被允许在Ajax请求中。

+0

我试图使用encodeURIComponent,它转换的“&”,而不是“@”,所以它仍然没有奏效。另外,不需要在IE8中测试它...它是一个Palm WebOS应用程序,所以它只会在那里运行... – 2010-07-21 12:13:12

+0

@sunmorgus奇怪 - 它绝对应该编码'@'。但也许它会被解释为URL的一部分。 – 2010-07-21 12:16:25

相关问题