发出新请求时出现未定义的错误

时间:2013-09-06 08:11:29

标签: jquery

当用户导航到其他链接时,我面临未定义的错误。我在页面加载时触发6-7 jquery它在加载时工作正常。当用户连续刷新页面或点击其他链接时,它显示错误。我认为之前请求的错误bcoz没有完成。我在互联网上找到了这个有用的技巧

 // Automatically cancel unfinished ajax requests 
// when the user navigates elsewhere.
(function($) {
  var xhrPool = [];
  $(document).ajaxSend(function(e, jqXHR, options){
    xhrPool.push(jqXHR);
  });
  $(document).ajaxComplete(function(e, jqXHR, options) {
    xhrPool = $.grep(xhrPool, function(x){return x!=jqXHR});
  });
  var abort = function() {
    $.each(xhrPool, function(idx, jqXHR) {
      jqXHR.abort();
    });
  };

  var oldbeforeunload = window.onbeforeunload;
  window.onbeforeunload = function() {
    var r = oldbeforeunload ? oldbeforeunload() : undefined;
    if (r == undefined) {
      // only cancel requests if there is no prompt to stay on the page
      // if there is a prompt, it will likely give the requests enough time to finish
      abort();
    }
    return r;
  }
})(jQuery);
来自http://stackoverflow.com/questions/1802936/stop-all-active-ajax-requests-in-jquery

它在谷歌浏览器和Opera中运行良好,但不能在Internet Explorer和Mozilla中工作。任何人都可以告诉我如何更改此代码以支持所有浏览器。

0 个答案:

没有答案