2

我目前有一个 Palm WebOS 应用程序,它使用 Ajax.Request 连接到使用基本身份验证的 Web 服务。要发送用户名和密码,我只需将其包含在 url 中(即http://username:password@ip-address:port/),它工作得非常好,期望密码包含除字母数字字符以外的任何内容(例如,我最近有一个用户给我发了电子邮件,他的密码中包含一个“@”和一个“&”,他无法连接,因为这些符号没有被正确解析为 url)。有什么办法可以在 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": ""
4

2 回答 2

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 }
});
于 2010-07-21T12:24:50.240 回答
0

您可以尝试在使用encodeURIComponent.

更一般地说,您是否在 IE8 中测试过这种方法?因为它不再适用于正常请求 -username:password@出于安全原因,该方案已被禁用。

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

于 2010-07-21T12:03:10.060 回答