我目前有一个Palm WebOS应用程序,它使用Ajax.Request连接到使用基本身份验证的Web服务。要发送用户名和密码,我只需将其包含在网址(即http://username:password@ip-address:port/)中,该网址效果非常好,当密码包含除字母数字字符以外的任何内容时(例如,我有一位用户最近给我发送电子邮件的人)在他的密码中包含一个“@”和一个“&”,他无法连接,因为符号没有被正确解析为url)。有没有办法在网址中发送凭据,以便我可以允许用户在密码中使用除字母数字之外的其他内容?
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": ""
答案 0 :(得分: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 }
});
答案 1 :(得分:0)
您可以尝试在使用encodeURIComponent
发送用户名和密码之前对其进行编码。
但更普遍的是,您是否在IE8中测试了此方法?因为它不再适用于正常请求 - 出于安全原因,username:password@
方案已被禁用。