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

前端 未结 3 1575
误落风尘
误落风尘 2021-01-05 12:40

jQuery:

$.ajax({
url : url,
type : \'GET\',
dataType: \'json\',
data: {
    \'FN\'    : \'GetPages\',
    \'PIN\'   : \'7659\' 
},
xhrFields         


        
相关标签:
3条回答
  • 2021-01-05 13:24

    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

    Try to change dataType: 'json' to dataType: 'jsonp'

    and add callback=? to your URL

    0 讨论(0)
  • 2021-01-05 13:29

    Thanks to @CrimsonChin I know its a Same Origin Policy problem

    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]

    (from http://en.wikipedia.org/wiki/Same_origin_policy)

    Granting JavaScript clients basic access to your resources simply requires adding one HTTP response header, namely:

    Access-Control-Allow-Origin: *
    Access-Control-Allow-Origin: http://foo.example.com
    

    (from http://enable-cors.org/)

    Ofc, turning the JSON response into a JSONP response would also work. Thx @djakapm

    0 讨论(0)
  • 2021-01-05 13:33

    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));
                    }
              });
    
    0 讨论(0)
提交回复
热议问题