我尝试使用 jquery ajax 从其他域检索 json 数据,但它不起作用。这是我的代码:
function getLeague() {
$.ajax({
url: 'http://otherdomainurl.ashx?username=xxx&pass=xxx&type=xxx',
headers: { 'Access-Control-Allow-Origin': '*' },
dataType: 'jsonp',
async: false,
crossDomain: true,
success: function(data) {
alert('Success');
},
error: function(error) {
alert('Fail');
}
});
}
我尝试删除 header 、异步和跨域。我尝试将数据类型更改为 json。但它总是发出失败警报。我使用 django(但我认为这不是问题)。谢谢..
最佳答案
此 header 需要位于服务器端,而不是客户端。
A Django App that adds CORS (Cross-Origin Resource Sharing) headers to responses.
Although JSON-P is useful, it is strictly limited to GET requests. CORS builds on top of XmlHttpRequest to allow developers to make cross-domain requests, similar to same-domain requests.
关于javascript - 使用jquery ajax跨域检索json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35595088/