JQuery - $ .ajax() - 使用JSONP的跨源 - 仅在IE 8(在IE 7中工作)获取'parsererror' [英] JQuery - $.ajax() - Cross-origin using JSONP - Getting 'parsererror' only in IE 8 (working in IE 7)

查看:419
本文介绍了JQuery - $ .ajax() - 使用JSONP的跨源 - 仅在IE 8(在IE 7中工作)获取'parsererror'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来执行跨域请求并获取JSONP数据(JSON通过回调方法包装)。我已经验证了我正在得到的响应回调方法包装我的JSON数据。

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



我得到statusText为成功,StatusCode为200 。另外,我不能找到任何正确地称为responseText xhr。那么我如何得到在错误/完整的函数的响应?任何想法?

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?

推荐答案

Jquery自动传递一个回调类似 callback = JQuery132123412415235 并且服务器必须返回一个脚本调用此函数与数据 JQuery132123412415235(data_returned),其余的等于标准的json请求

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

您还使用成功和错误属性并使用promise和错误(function(data)) complete data))只是为了一个明确的代码我认为你必须只使用一个方法。代码如下:

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

});

请记住,服务器必须以callback_function(data)格式返回数据,其中数据是json对象如果你在一个标准的json调用返回。

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.

这篇关于JQuery - $ .ajax() - 使用JSONP的跨源 - 仅在IE 8(在IE 7中工作)获取'parsererror'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆