Ajax with JQuery: 200 ok, but not “success”

前端 未结 3 1009
耶瑟儿~
耶瑟儿~ 2021-02-04 22:27

I\'m facing the same issue, while trying for cross-domain request. Situation is strange, my data is getting loaded while hit the requested url directly on browser, the strange p

相关标签:
3条回答
  • 2021-02-04 23:05

    Attempted cross domain XMLHttpRequest requests may fool you. In Firefox web console, it may look like URL loaded fine, but body is an empty string.

    Confirm that the server supports JsonP. If you don't really know what that means, you need to look it up. It is critically important.

    jQuery assumes that the JsonP parameter is going to be "?callback=". If that is not true, you should see this: http://api.jquery.com/jQuery.ajax/

    jsonp: Override the callback function name in a jsonp request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

    jsonpCallback: Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.

    If it gets confusing, it may be easier to just do it the old fashioned way and appends the script to the page yourself with a time stamp in the URL to avoid using a cached script page.

    BTW, AFAIK, there is no way to combine JSONP and POST. JSONP is a way of working around the same origin security policy of XMLHttpRequest. It requires you to append a script to the DOM. I don't think you can do that and also submit POST variables as part of the process.

    0 讨论(0)
  • 2021-02-04 23:12
    function fetchJsonData() {
    $.ajax({
        type: 'POST',
        url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%20'www.meilleurmobile.com%2Fcomparateur%2Fresultats-comparateur-json.do%3FmonthDur%255B0%255D%3D45.75%26monthDur%255B1%255D%3D45.75%26monthDur%255B2%255D%3D45.75%26monthDur%255B3%255D%3D45.75%26monthDur%255B4%255D%3D45.75%26monthDur%255B5%255D%3D45.75%26monthDur%255B6%255D%3D45.75%26monthDur%255B7%255D%3D45.75%26monthDur%255B8%255D%3D45.75%26monthDur%255B9%255D%3D45.75%26monthDur%255B10%255D%3D45.75%26monthDur%255B11%255D%3D45.75%26numSms%3D1000%26dataVolume%3D1000%26withoutMobile%3D-1%26commitmentDuration%3D-1%26_%3D1329825461536'&format=json&diagnostics=true",
        async: false,
        cache: false,
        crossDomain: true,
        dataType: 'jsonp',
        error: function( xhr,err ) {
            console.log( 'Sample of error data:', err );
            console.log("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\nresponseText: "+xhr.responseText);
        },
        success: function( data, status ) {
            if (console && console.log) {
                console.log( 'data count:', data.query.results.json.json.length );
                $('#result-count').text( JSON.stringify(data.query.results.json.json) );
            }
        },
        jsonpCallback: 'swatCallback'
    })
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); }); 
    }
    
    
    window['swatCallback'] = function(data) {
        var info = data.query.results.json.json;
        $('#callback_result_operator').html(info[0].operator);
    };
    
    0 讨论(0)
  • 2021-02-04 23:22

    try with

     console.log(data);
    

    with no slices.

    or, even better

     console.log(JSON.stringify(data.slice(0,100)));
    

    with JSON.stringify() (documented here) method you get a string wich is not parsed or checked for json validity.

    hope this helps.

    0 讨论(0)
提交回复
热议问题