jQuery Ajax成功/错误回调没有在IE中触发

时间:2012-06-28 21:37:50

标签: jquery ajax internet-explorer

我一直在尝试让ajax调用在IE9中正常工作。我可以在网络数据中看到请求通过正常。并且jQuery $ .ajax()调用返回的jqXHR对象包含响应数据。然而,我的成功/错误/完整回调没有解雇。这是代码......

在我的脚本顶部:

// override xhr for browser that use XDR
if ('XDomainRequest' in window && window.XDomainRequest !== null) {

  // override default jQuery transport for IE
  jQuery.ajaxSettings.xhr = function() {
      try { return new XDomainRequest(); }
      catch(e) {
        console.log('test'); 
      }
  };

  // Maybe a 304 response is causing the callbacks not to fire?
  // This makes sure I'm getting 200
  jQuery.ajaxSettings.cache = false;

  // also, override the support check
  jQuery.support.cors = true;
}

然后我的ajax电话......非常简单。

$.ajax({
    url: url,
    success: success,
    complete: complete,
    beforeSend: beforeSend,
    error: error
});

function success(response, textStatus, jqXHR){
    console.log('success');
    if( typeof fn == 'function'){
        fn(response.data);
    } else {
        console.log('Invalid callback supplied for dataGateway');
    }
}

function error(jqXHR, textStatus, errorThrown){
    console.log("Could not retrieve data from API.");
    return;
}

function complete(jqXHR, textStatus){
    console.log('complete');
}

// This is the only callback that gets fired
function beforeSend(jqXHR, settings){
    console.log('before send');
}

有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

不知道这是否有帮助,但这是我工作的ajax调用的一个例子。

    $.ajax({
        url: "http://myurl.com",
        type: "POST",
        dataType: "xml",
        data: GetCurrentUserSoapEnv, //my variable containing the xml I sending
        complete: processResult, //the function to call on completion
        contentType: "text/xml; charset=\"utf-8\""
});

答案 1 :(得分:0)

我知道这是一种与你所寻求的不同的方法,你可能已经解决了你的问题,但我最近处理类似的IE兼容性问题,我认为使用XDomainRequest的内置处理程序会更简单

为拒绝遵循标准的浏览器制作例外情况令人反感,我在项目中使用类似以下的内容来解释这种情况:

    // This is necessary due to IE<10 having no support for CORS.
function fallbackXDR(callObj) {
    if (window.XDomainRequest) { 
        var xdrObj = new XDomainRequest();
        xdrObj.timeout = callObj.timeout;
        xdrObj.onload = function() {
            success({data:xdrObj.responseText});
            complete();
        };
        xdrObj.onerror = function() {
            error(xdrObj);
        };
        xdrObj.ontimeout = function() {
            callObj.xdrAttempts = callObj.xdrAttempts++ || 1;
            if (callObj.xdrAttempts < callObj.maxAttempts) {
                fallbackXDR(callObj);
            }
        };
        xdrObj.onprogress = function() {
            // Unfortunately this has to be included or it will not work in some cases.
        };

        // Use something other than $.param() to format the url if not using jQuery.
        var callStr = callObj ? '?'+$.param(callObj.urlVars) : '';
        xdrObj.open("get", callObj.url+callStr);
        xdrObj.send();
    } else {
        handleError("No XDomainRequest available.", callObj);
    }
}//fallbackXDR()