简体   繁体   English

jQuery AJAX状态为“ 200 OK”,但无数据响应

[英]jQuery AJAX status “200 OK”, but no data response

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 Firefox网络

萤火虫错误

What happens 怎么了

The AJAX "error:" event gets triggered and my console.log outputs are: AJAX“错误:”事件被触发,我的console.log输出为:

xhr.status -> 0 xhr.status-> 0

thrownError -> (empty String) thrownError->(空字符串)

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内容的文件下载,这应该不是问题吧?

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. 在计算中,对于许多浏览器端编程语言(例如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] 该策略允许在源自同一站点的页面上运行的脚本不受特定限制地访问彼此的方法和属性,但可以阻止跨不同站点的页面访问大多数方法和属性。[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/ ) (来自http://enable-cors.org/

Ofc, turning the JSON response into a JSONP response would also work. Ofc,也可以将JSON响应转换为JSONP响应。 Thx @djakapm 谢谢@djakapm

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));
                }
          });

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=? 并添加callback=? to your URL 到您的URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM