jQuery AJAX 状态“200 OK”,但没有数据响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13525097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery AJAX status "200 OK", but no data response
提问by user1841515
jQuery:
jQuery:
$.ajax({
url : url,
type : 'GET',
dataType: 'json',
data: {
'FN' : 'GetPages',
'PIN' : '7659'
},
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function(data) {
alert('succsess');
console.log('data', data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error');
console.log(xhr.status);
console.log(thrownError);
}
});
Firebug Firefox Network
Firebug 火狐网络
What happens
发生什么了
The AJAX "error:" event gets triggered and my console.logoutputs are:
AJAX "error:" 事件被触发,我的console.log输出是:
xhr.status -> 0
thrownError -> (empty String)
xhr.status -> 0
throwError -> (空字符串)
Is this normal? When I type the URL in a browser I receive a file download with the JSON content in it, this shouldn't be a problem right?
这是正常的吗?当我在浏览器中输入 URL 时,我会收到一个包含 JSON 内容的文件下载,这应该不是问题吧?
回答by user1841515
Thanks to @CrimsonChin I know its a Same Origin Policy problem
感谢@CrimsonChin 我知道这是同源政策问题
In computing, the same origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy permits scripts running on pages originating from the same site to access each other's methods and properties with no specific restrictions, but prevents access to most methods and properties across pages on different sites.[1]
在计算中,同源策略是许多浏览器端编程语言(例如 JavaScript)的重要安全概念。该策略允许在来自同一站点的页面上运行的脚本在没有特定限制的情况下访问彼此的方法和属性,但阻止跨不同站点的页面访问大多数方法和属性。[1]
(from http://en.wikipedia.org/wiki/Same_origin_policy)
(来自http://en.wikipedia.org/wiki/Same_origin_policy)
Granting JavaScript clients basic access to your resources simply requires adding one HTTP response header, namely:
授予 JavaScript 客户端对您的资源的基本访问权限只需添加一个 HTTP 响应标头,即:
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: http://foo.example.com
(from http://enable-cors.org/)
Ofc, turning the JSON response into a JSONPresponse would also work. Thx @djakapm
Ofc,将 JSON 响应转换为JSONP响应也可以。谢谢@djakapm
回答by gurusai
Try this one
试试这个
$.ajax({ type : "GET",
url : URL,
data: {
'FN' : 'GetPages',
'PIN' : '7659'
},
xhrFields: {
withCredentials: true
},
crossDomain: true,
dataType : "jsonp",
jsonp : "jsoncallback",
jsonpCallback : "SMS",
cache : true,
success : function(service_data) {
},
error : function(msg) {
alert(JSON.stringify(msg));
}
});
回答by djakapm
I cant figured out from the image, but if you are trying send AJAX request to different domain, you need to use JSONP, simple AJAX request will not be sufficient
我无法从图像中弄清楚,但是如果您尝试将 AJAX 请求发送到不同的域,则需要使用JSONP,简单的 AJAX 请求是不够的
Try to change dataType: 'json'
to dataType: 'jsonp'
尝试更改dataType: 'json'
为dataType: 'jsonp'
and add callback=?
to your URL
并添加callback=?
到您的URL