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

时间:2022-11-29 13:35:20

I use jQuery 1.2.6 (legacy).

我使用jQuery 1.2.6(遗留)。

In my general configuration I have set these options:

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

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

I have an ajax function that gets a javascript file:

我有一个获取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");
}   

All works great when the ajax get response correctly: the ajaxStart and the ajaxStop events are triggered. But when I got a 404 error the error callback function is not called neither the ajaxStop event: in this case I do not receive any error but my page remains freeze since the ajaxStart is triggered and the blockUI function is executed.

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

Is there a way to handle this kind of situation? I know that on jquery 1.5 there's the statusCode option, but I have to make it work on my legacy version.

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

Kind regards

Massimo

1 个解决方案

#1


2  

as pointed out in the comments by @Massimo Ugues: statuscode is not present in jQuery 1.2.6. It's present on jquery >1.5

正如@Massimo Ugues的评论中所指出的:jQuery 1.2.6中没有statuscode。它存在于jquery> 1.5


use the statusCode (present in jquery 1.5+)

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

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

also you can take the statusCode to the ajaxSetup

你也可以把statusCode带到ajaxSetup

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

#1


2  

as pointed out in the comments by @Massimo Ugues: statuscode is not present in jQuery 1.2.6. It's present on jquery >1.5

正如@Massimo Ugues的评论中所指出的:jQuery 1.2.6中没有statuscode。它存在于jquery> 1.5


use the statusCode (present in jquery 1.5+)

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

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

also you can take the statusCode to the ajaxSetup

你也可以把statusCode带到ajaxSetup

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