【问题标题】:Jquery $.get or $.ajax not working in Internet ExplorerJquery $.get 或 $.ajax 在 Internet Explorer 中不起作用
【发布时间】:2012-10-19 07:07:27
【问题描述】:

我一直在 IE 9 中运行此代码,但运气不佳。我查看了所有关于 UTF-8 修复等的帖子,但无济于事。有什么想法吗?

$.get({
    url: 'http://api.flickr.com/services/rest/?api_key={apikey}&method=flickr.collections.getTree&user_id=66970820%40N03&collection_id=66947766-72157631850748939',
    success: function () {
        console.log('success!');
    }
}).done(function () {
    console.log('done');
}).fail(function () {
    console.log('fail')
});

它在 Safari、FF 和 Chrome 中运行良好。将URL粘贴到IE中时,响应很好。

【问题讨论】:

  • 它到底是怎么不工作的?
  • 在不了解全部情况的情况下,我会说您可能会遇到跨域问题。尝试查看 $.getJSON。特别是关于 JSONP 的部分。
  • URL 是无法跨域检索的 xml 文档...您需要将 jsonp dataType 添加到 $.get 并在 url 中为 jsonp 设置参数,以便 API 返回 jsonp 而不是 xml
  • IE8-9 不支持使用标准 ajax 方法的 CORS,这很可能是问题所在。

标签: jquery ajax internet-explorer


【解决方案1】:

@Iden Gozlan,你的回答听起来不错,但我虚弱的头脑很混乱。

@Erik 和@charlietfl 你对 JSONP 的建议让我走上了正确的道路。这绝对是一个跨域脚本问题。无法理解为什么 IE 是唯一不允许这样做的。我这样编辑了我的代码,一切都很好!

$.ajax({
  url: 'http://api.flickr.com/services/rest/?api_key={apikey}&method=flickr.collections.getTree&user_id=66970820%40N03&collection_id=66947766-72157631850748939&jsoncallback=doSomeGreatStuff',
  dataType: "jsonp"
});

function doSomeGreatStuff(response) {
  // do some great stuff with the json response
  console.log( response.collections.collection[0].id );
}

帮助我的资源是 herehere 甚至是 here

【讨论】:

    【解决方案2】:

    这个jQuery XDomainRequest plugin 创造了奇迹。
    我在使用 IE8 和 9 时遇到了 ajax 问题,但在不更改任何代码的情况下简单地包含这个插件就为我提供了 IE8 和 9 CORS ajax 功能:)

    【讨论】:

      【解决方案3】:

      它的已知问题,请阅读这篇文章: IE9 jQuery AJAX with CORS returns "Access is denied"

      您应该使用 XMLHttpRequest 原始调用或下载以下插件,它将为您提供解决方案:

      https://github.com/jaubourg/ajaxHooks/blob/master/src/xdr.js

      【讨论】:

        猜你喜欢
        • 2012-02-23
        • 2012-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多