我使用 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 选项,但我必须让它在我的旧版本上工作。
亲切的问候
马西莫
最佳答案
<小时/>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
使用statusCode (存在于 jquery 1.5+ 中)
$.ajax({
statusCode: {
404: function() {
alert('page not found');
}
}
});
您也可以将 statusCode
带到 ajaxSetup
$.ajaxSetup({
statusCode: {
404: function() {
alert('page not found');
}
}
});
关于jQuery ajax 不会在 404 错误上触发 ajaxStop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9514622/