【问题标题】:Callback not getting called on Internet Explorer for Ajax JSONP call没有在 Internet Explorer 上调用 Ajax JSONP 调用的回调
【发布时间】:2013-05-20 23:20:42
【问题描述】:

我正在使用 JSONP 进行 Ajax 调用以从第三方(与我的站点不同的域)获取 JSON 数据,它在 Chrome 和 Firefox 上运行但在 IE 上失败(9 和 10 是我尝试过的版本) .在 IE 调试器中,我看到调用正在完成(使用 Http 200),但没有调用我为 JSONP 调用指定的回调。

我的代码如下所示:

$.ajax({
    url: 'http://api.yipit.com/v1/deals/?key=mykey&division=seattle&limit=1000',
    dataType: 'jsonp',
    contentType: "text/javascript",
    async: false,
    cache: false,
    crossDomain: true,
    jsonp: 'callback',
    jsonpCallback: 'processResult',
    error: function (xhr, textStatus, error) {
        window.alert(error);
    },
    success: function (data) {
        window.alert(JSON.stringify(data));
        _yipit_deals = data.response.deals;
    }
});

响应正文如下所示:

<html><body><pre>processResult({
... [valid JSON data]
});</pre></body></html>

调用时,错误函数被调用,错误为:Error: processResult was not called,IE 调试器显示在尝试解析响应中的&lt;html&gt;&lt;body&gt;&lt;pre&gt; 标记时导致的脚本错误。在 Chrome 和 Firefox 上运行时,这些 html 标签 存在于响应正文中,我不确定为什么 IE 的响应不同。

这些标签似乎导致 IE 出错并且无法调用回调。我尝试为 contentType 指定其他值,例如“text/html”、“text”、“application/javascript”,甚至根本没有指定它,但它没有任何区别。我正在使用 JSONP 来解决跨域问题。

有什么想法吗?谢谢!

【问题讨论】:

    标签: jsonp


    【解决方案1】:

    您必须将明确的 format 参数传递给 URL:

    &format=json
    

    否则,它只是漂亮地打印输出:

    <html><body><pre>{
        "meta": {
            "code": 401,
            "message": "Key not recognized",
            "name": "AuthenticationFailed"
        },
        "response": {}
    }</pre></body></html>
    

    另外,我会将async 设置回true

    【讨论】:

    • 解决了!谢谢一堆。是的,我会将 async 设置回 true。这个问题让我发疯了,再次感谢!
    • 澄清一下,将&amp;format=json 添加到 URL 为我解决了这个问题。将contentType 设置为application/json 无效。
    • 更改为 async=true 帮助了我。谢谢。
    猜你喜欢
    • 2016-07-02
    • 1970-01-01
    • 2012-06-25
    • 2012-09-12
    • 2012-01-23
    • 1970-01-01
    • 2013-04-25
    • 2016-07-01
    • 1970-01-01
    相关资源
    最近更新 更多