我在我的网络服务器上使用HTTP Basic Auth。现在我想通过jQuery调用PHP脚本。如果没有Basic Auth,它可以正常工作,但激活Basic Auth后,浏览器会显示登录提示。对我来说,似乎Auth参数没有正确传输?!有什么想法吗?
这是我的js:
var username = [USER];
var password = [PASSWORD];
$.ajax
({
type: "GET",
url: "[URL]/[Script].php?callback=?",
dataType: 'json',
headers: {
"Authorization": "Basic " + btoa(username +":"+ password)
},
success: function(result){
alert('Yeah!');
},
error: function(er){
alert('error!' +er);
}
});
编辑,但也不行:/
$.ajax
({
type: "GET",
url: "[URL]/[Script].php?callback=?",
dataType: 'json',
async: false,
xhrFields: {
withCredentials: true
},
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},
success: function(result){
alert("YEY");
},
error: function(er){
alert('error!' +er);
}
});
No Auth: http://fs5.directupload.net/images/161220/izy9yqdh.jpg
正确认证: http://fs5.directupload.net/images/161220/nkh6ybts.jpg
直接php调用: http://fs5.directupload.net/images/161220/xvo4rry9.jpg