【问题标题】:JQuery - $.ajax() - Cross-origin using JSONP - Getting 'parsererror' only in IE 8 (working in IE 7)JQuery - $.ajax() - 使用 JSONP 的跨域 - 仅在 IE 8 中获取“解析器错误”(在 IE 7 中工作)
【发布时间】:2011-12-31 05:19:09
【问题描述】:

我有以下代码来执行跨域请求并获取 JSONP 数据(由回调方法包装的 JSON)。我已经验证我使用包含我的 JSON 数据的回调方法正确地获得了响应。它在 IE7 中完美运行(回调 cb 被调用),但在 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);
}

我在 xhr 中将 statusText 设为“Success”,将 StatusCode 设为 200。此外,我无法为 xhr 找到任何正确称为 responseText 的内容。那么如何在错误/完整功能中获得响应?有什么想法吗?

【问题讨论】:

    标签: jquery internet-explorer-8 cross-domain jsonp


    【解决方案1】:

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

    您还使用成功和错误属性并使用承诺和error(function (data) ) 和complete(function (data)) 只是为了获得清晰的代码我认为您必须只使用一种方法。代码是这样的:

    $(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 调用中返回一样。

    【讨论】:

      猜你喜欢
      • 2011-02-13
      • 1970-01-01
      • 2010-12-01
      • 2011-11-30
      • 2013-07-07
      • 2011-03-22
      • 1970-01-01
      • 2012-02-25
      相关资源
      最近更新 更多