Https 如何使用基本身份验证发出dojo.request.xhr GET请求
我看了一下文档以了解更多信息 我找不到包含基本身份验证的示例 如何以及在何处将用户名和密码包含在Dojo XHR选项中Https 如何使用基本身份验证发出dojo.request.xhr GET请求,https,dojo,xmlhttprequest,Https,Dojo,Xmlhttprequest,我看了一下文档以了解更多信息 我找不到包含基本身份验证的示例 如何以及在何处将用户名和密码包含在Dojo XHR选项中 require(["dojo/request/xhr"], function(xhr){ xhr("example.json", { // Include User and Password options here ? user: "userLogin" password: "userPassword" handleAs: "json"
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
});
});
谢谢。实际上,您应该能够使用
选项
对象中的用户
和密码
属性传递用户和密码
在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;
};
var toByteArray=函数(str){
var字节=[];
对于(变量i=0;i