我需要联系服务器以使用 HTTP 服务。
尝试使用浏览器访问服务,使用 https://example.com/service
我得到基本的身份验证对话框。
将 URL 更改为 https://username:password@example.com/service
很容易绕过它。
尽管尝试使用 Ajax 执行相同的操作总是会导致 401 Unauthorized
:
$.ajax({
url: 'https://username:password@example.com/service',
// rest repeats
type: "GET",
async: true,
success: function(text) { alert('success'); },
error: function (text) { alert('error') },
complete: function(text) { alert('complete'); }
});
$.ajax({
url: 'https://example.com/service',
username: username,
password: password,
// snip repeat
});
$.ajax({
url: 'https://example.com/service',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic "
+ btoa(username + ":" + password));
},
// snip repeat
});
最佳答案
我自己也遇到过类似的情况.. 诀窍是使用 jsonp(呃):
$.ajax({
url: "https://localhost:8443/v1/accounts",
type: 'GET',
dataType: 'jsonp',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic bHVpZ2lAZ21haWwuY29tOmFiYzEyMzQ1');
}
})
关于jquery - 通过 Ajax 调用使用 http 基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27904082/