我查看文档Dojo v.1.9 请求/xhr http://dojotoolkit.org/reference-guide/1.9/dojo/request/xhr.html#dojo-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
});
});
Thanks.
事实上,您应该能够使用以下命令传递用户名和密码user
and password
财产在options
object.
在 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;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)