[英]Retrieve json data with jquery ajax cross domain
I try to retrieve json data from other domain with jquery ajax, but it doesn't work. 我尝试使用jquery ajax从其他域检索json数据,但是它不起作用。 This is my code: 这是我的代码:
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');
}
});
}
i've tried to remove header, async, and crossDomain. 我试图删除标头,异步和crossDomain。 i've tried to change the dataType to json. 我试图将dataType更改为json。 But it always give a fail alert. 但是它总是发出失败警报。 I use django(but i think it's not the problem). 我使用django(但我认为这不是问题)。 Thanks.. 谢谢..
This header needs to be on the server side, not client side. 此标头必须在服务器端,而不是客户端。
Try Django CORS Headers : 试试Django CORS标头 :
A Django App that adds CORS (Cross-Origin Resource Sharing) headers to responses. 一个Django应用程序,它将CORS(跨源资源共享)标头添加到响应中。
Although JSON-P is useful, it is strictly limited to GET requests. 尽管JSON-P很有用,但它严格限于GET请求。 CORS builds on top of XmlHttpRequest to allow developers to make cross-domain requests, similar to same-domain requests. CORS建立在XmlHttpRequest之上,以允许开发人员发出跨域请求,类似于同域请求。
Your error is at async: false . 您的错误是异步的:false 。
Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation 跨域请求和dataType:“ jsonp”请求不支持同步操作
http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/
So. 所以。 do your really need to set async: false? 您真的需要设置async:false吗?
Try Below code 尝试以下代码
function getLeague() {
$.ajax({
url: 'http://otherdomainurl.ashx',
data: {Your Obj},
type: 'GET',
dataType: 'jsonp',
success: function(data) {
alert('Success');
},
error: function(error) {
alert('Fail');
}
});
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.