我正在尝试从 appannie.com 的 api 获取一些数据,但似乎无法让基本身份验证正常工作。我得到的只是 401(未经授权)错误。这是我所拥有的:
var username = "email@email.com";
var password = "password";
var url = "https://api.appannie.com/v1/accounts";
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return "Basic " + hash;
}
$.ajax({
url: url,
dataType: "jsonp",
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', make_base_auth(username, password));
xhr.setRequestHeader('Accept', "application/json");
},
success: function(data) {
alert("success");
},
fail: function(data) {
alert("fail");
}
});