JQuery - $.ajax() - Cross-origin using JSONP - Getting 'parsererror' only in IE 8 (working in IE 7)

倖福魔咒の 提交于 2019-12-23 10:22:23

问题


I've the following code to do a crossdomain request and get JSONP data (JSON wrapped with by callback method). I've verified that I'm getting the response correctly with the callback method wrapping my JSON data. It is working PERFECTLY in IE7 (the callback cb is getting called) but not in IE8.

    $(document).ready(function () {
    var abc = $.ajax({
        type: "GET",
        url: "http://sd.domain.com/param1=a&param2=b&output=json&callback=cb",
        dataType: "jsonp",
        jsonp: false,
        cache: false,
        success: function (json) {

        },
        error: function (e) {

        }
    });

    abc.error(function (data, xhr, dat1) {

    });

    abc.complete(function (xhr, status) {
        var data = xhr.responseText;
    });
});

function cb(dd) {
    alert(dd.people[0].nameFirst);
}

I'm getting the statusText as 'Success' and StatusCode as 200 in xhr. Also I'm not able to find any propertly called responseText for xhr. So how can I get the response in the error/complete functions? Any ideas?


回答1:


Jquery automatic pass a callback something like callback=JQuery132123412415235 and the server must return a script calling this function with the data JQuery132123412415235(data_returned) and the rest is equal to the standard json request

You also use the success and error properties and use the promise and error(function (data) ) and complete(function (data)) just for a clear code I think you must use only one method. The code is like this:

$(document).ready(function () {
    var abc = $.ajax({
        type: "GET",
        url: "http://sd.domain.com/param1=a&param2=b&output=json",
        dataType: "jsonp",
        jsonp: false,
        cache: false
    });

    abc.error(function (data, xhr, dat1) {

    });

    abc.complete(function (xhr, status) {
        var data = xhr.responseText;
    });

    abc.done(data){
       //alert(data.people[0].nameFirst); ?????        
    }

});

Remember the server must return the data in the form callback_function(data) where data is the json object like if you returned in a standard json call.



来源:https://stackoverflow.com/questions/8165557/jquery-ajax-cross-origin-using-jsonp-getting-parsererror-only-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!