微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

https – 如何使用基本身份验证进行dojo.request.xhr GET请求

我看一下 Dojo v.1.9 request/xhr的文档
我找不到包含基本身份验证的示例.

如何以及在何处在Dojo XHR选项中包含用户名密码

require(["dojo/request/xhr"],function(xhr){
  xhr("example.json",{
    // Include User and Password options here ?
    user: "userLogin"
    password: "userPassword"
    handleAs: "json"
  }).then(function(data){
    // Do something with the handled data
  },function(err){
    // Handle the error condition
  },function(evt){
    // Handle a progress event from the request if the
    // browser supports XHR2
  });
});

谢谢.

解决方法

实际上,您应该能够使用options对象中的user和password属性传递用户密码.

在以前的Dojo版本中,这是有记录的,但现在似乎并非如此.但是,我刚刚测试了它,似乎在URL中添加用户名密码,例如:

http://user:password@myUrl/example.json

通常,浏览器应该能够翻译此URL,以便设置请求标头.

您也可以手动设置这些标头,例如使用:

xhr("example.json",{
    headers: {
        "Authorization": "Basic " + base64.encode(toByteArray(user + ":" + pass))
    }
}).then(function(data) {
    // Do something 
});

但是,这需要dojox / encoding / base64模块和以下功能

var toByteArray = function(str) {
    var bytes = [];
    for (var i = 0; i < str.length; ++i) {
        bytes.push(str.charCodeAt(i));
    }
    return bytes;
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐