小编典典

对Ajax.Request使用身份验证

ajax

我目前有一个Palm
WebOS应用程序,该应用程序使用Ajax.Request通过基本身份验证连接到Web服务。要发送用户名和密码,我只需将其包含在url(即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": ""

阅读 221

收藏
2020-07-26

共1个答案

小编典典

发送带有标题的基本身份验证信息: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 }
});
2020-07-26