[英]Jquery - ajax beforeSend basic authentication not working as expected
I am trying to send a request using ajax to a server which is protected by basic authentication with the following code: 我正在尝试使用ajax将请求发送到受以下基本代码保护的服务器:
$('document').ready(function(){
$.ajax({
url: "https://somesite.com/somefolder",
type: "POST",
dataType: 'jsonp',
beforeSend: function(xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa('myusername' + ":" + 'mypassword'));
},
success: function(data){
console.log('SUCCESS!');
},
error: function () {
console.log("error");
}
});
});
So I provide the credentials in the beforeSend so my expectation would be that there would be no credential popup from the browser since I already provided the credentials but unfortunately when i run this code I get the popup to enter my credentials. 因此,我在beforeSend中提供了凭据,因此我的期望是浏览器不会弹出凭据,因为我已经提供了凭据,但是不幸的是,当我运行此代码时,我得到了弹出窗口以输入凭据。 I want to the code to provide these credentials. 我想在代码中提供这些凭据。
Similar to this question , example using headers: 与此问题类似,使用标头的示例:
$.ajax({
url: "https://somesite.com/somefolder",
type: "POST",
dataType: 'jsonp',
headers: {"Authorization": "Basic " + btoa('myusername' + ":" + 'mypassword')},
success: function(data){
console.log('SUCCESS!');
},
error: function () {
console.log("error");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.