Javascript 使用具有身份验证的JQuery访问JSON
我想从这里接收json,但它具有用户名Nama pengguna是foo和密码Sandi foo的身份验证。我如何在我的程序中接收它?我使用Jquery和eant来构建phonegap和Android 我正在这样努力Javascript 使用具有身份验证的JQuery访问JSON,javascript,android,jquery,cordova,Javascript,Android,Jquery,Cordova,我想从这里接收json,但它具有用户名Nama pengguna是foo和密码Sandi foo的身份验证。我如何在我的程序中接收它?我使用Jquery和eant来构建phonegap和Android 我正在这样努力 $.ajax({ url: 'http://api.tabloidnova.com/1/subscribe/get?api_key=259225f04f4015746b03e1bad6238eaa&format=json&channe
$.ajax({
url: 'http://api.tabloidnova.com/1/subscribe/get?api_key=259225f04f4015746b03e1bad6238eaa&format=json&channel_id=110',
beforeSend: function(xhr){
xhr.setRequestHeader("Authorization","Basic foo:foo");
},
dataType: 'json',
async: false,
success: function(data){
$.each(data,function(i,grab){
console.log(grab[i].article.node_id);
console.log("tes");
})
}
})
但在我的ripple模拟器中,它仍然会给我未经授权的错误。如果不使用ripple emulator,它将无法工作,因为我从具有源策略的localhost运行它。有什么建议吗
现在解决了。最后,我首先将其编码为我的用户名和密码。感谢javascript中的,使用ajax我们可以这样做:
$.ajax({
'url': 'http://host.com/action/',
'beforeSend': function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + encodeBase64(username + ":" + password) //May need to use "Authorization" instead
},
sucess: function(result) {
alert('done');
}
});
检查文档中是否有错误。您将有更多选项来实现此功能。您需要设置适当的请求头以传递凭据。例如,见 可能重复的
$.getJSON({
'url': 'http://host.com/action/',
'otherSettings': 'othervalues',
'beforeSend': function(xhr) {
//May need to use "Authorization" instead
xhr.setRequestHeader("Authentication",
"Basic " + encodeBase64(username + ":" + password)
},
sucess: function(result) {
alert('Done');
}
});