【问题标题】:Error in Ajax callback only in IE8?仅在 IE8 中出现 Ajax 回调错误?
【发布时间】:2015-05-01 05:51:12
【问题描述】:
  $.ajax({
    type: 'GET',
    url: 'myUrl/etc'
    dataType: 'jsonp',
    jsonp: 'callback',
    jsonpCallback: 'callback',
    error: function(error) {
      alert('Error');
      console.log(JSON.stringify(error));
    },
    timeout: 45000
  });

  function callback(result) {
    // Do what i want
  }

人。我只在 IE8 的 ajax 回调中收到错误,我不知道可能是什么或为什么会发生。

当我尝试在所有浏览器中运行 ajax 时,回调将被调用,但在 IE 中会弹出警报错误并显示 console.log:

LOG: {
 "readyState": "ok",
 "status": 200,
 "statusText": "success"
}

知道为什么只在 IE8 中给出这个“错误”吗???

【问题讨论】:

  • 您使用的是什么版本的 jQuery? jQuery 2.x 不支持 IE8。 1.x 系列的最新版本仍然支持 IE8。
  • alert() 在某些版本的 IE 中会做一些奇怪的事情,有时会停止代码的执行。使用console.log() 然后回复我。
  • 我正在使用 1.x @jfriend00
  • 仅供参考,设置jsonpCallback: 'callback', 很危险。你为什么这样做?这指示 jQuery 使用名为 callback 的全局函数作为 jsonp 回调函数,而不是让它为每个 ajax 调用生成唯一的函数名。这也意味着您不能同时进行多个这样的 JSONP ajax 调用,并且必须得到正确的结果。
  • 另外,jsonp: 'callback', 已经是默认值,因此不需要。

标签: javascript jquery ajax internet-explorer-8


【解决方案1】:

这个问题已经解决了

在 AJAX 调用之前将其添加到脚本中(仅经过验证 在 IE8 中,而不是其他 IE):

jQuery.ajaxSetup({
        xhr: function() {
                //return new window.XMLHttpRequest();
                try{
                    if(window.ActiveXObject)
                        return new window.ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) { }

                return new window.XMLHttpRequest();
            }
    });

Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多