【问题标题】:Using Authentication with Ajax.Request通过 Ajax.Request 使用身份验证
【发布时间】:2011-03-18 22:52:32
【问题描述】:

我目前有一个 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": ""

【问题讨论】:

    标签: javascript ajax authentication webos ajax.request


    【解决方案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 }
    });
    

    【讨论】:

    • 好的,试过了,我收到 401 错误...用我的 Ajax.Request 调用和回复编辑了我的帖子...
    • 好的,我搞定了!我不得不将“标题”更改为“请求标题”,一切都很好!谢谢!
    【解决方案2】:

    您可以尝试在使用encodeURIComponent 发送用户名和密码之前对其进行编码。

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

    不过,它们可能仍然在 Ajax 请求中被允许。

    【讨论】:

    • 我尝试了 encodeURIComponent,它转换了“&”而不是“@”,所以它仍然不起作用。此外,无需在 IE8 中进行测试...它是一个 Palm WebOS 应用程序,所以它只会在那里运行...
    • @sunmorgus 很奇怪 - 它绝对应该编码 @。但也许它仍然被解释为 URL 的一部分。
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 2020-12-02
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多