jQuery ajax不会在404错误上触发ajaxStop

时间:2012-03-01 10:34:54

标签: jquery ajax http-status-code-404

我使用jQuery 1.2.6(遗留)。

在我的常规配置中,我设置了以下选项:

jQuery(document).ajaxStart(blockUI).ajaxStop(jQuery.unblockUI);

我有一个获取javascript文件的ajax函数:

function initWebtrends() {
    console.debug("initWebtrends start");       

    var options = {
        url : "ajax/myjavascript.js",
        success: function(data) {                   
            console.debug("webtrends integration successfully done...");
        },
        error:function(msg) {
            console.debug("error contacting webtrends client component...");                
        }
    };

    jQuery.ajax(options);

    console.debug("initWebtrends stop");
}   

当ajax正确获得响应时,一切正常:ajaxStart和ajaxStop事件被触发。 但是当我收到404错误时,错误回调函数既不会被调用ajaxStop事件:在这种情况下我没有收到任何错误但是我的页面仍然冻结,因为触发了ajaxStart并且执行了blockUI函数。

有没有办法处理这种情况? 我知道在jquery 1.5上有statusCode选项,但我必须让它在我的旧版本上运行。

亲切的问候

的Massimo

1 个答案:

答案 0 :(得分:2)

  

正如@Massimo Ugues的评论所指出的那样:状态代码不是   出现在jQuery 1.2.6中。它出现在jquery> 1.5


使用statusCode (存在于jquery 1.5 +中)

$.ajax({
  statusCode: {
    404: function() {
      alert('page not found');
    }
  }
});

您也可以将statusCode带到ajaxSetup

 $.ajaxSetup({
  statusCode: {
        404: function() {
          alert('page not found');
        }
      }    
   });