如果在别处处理,则防止调用.ajaxError

时间:2011-10-17 21:22:15

标签: javascript ajax jquery

我有一个全局$(document).ajaxError处理程序,可以处理大多数错误,以防万一出现意外的500错误或其他错误。但是,有时我想在脚本中本地捕获错误,然后阻止调用该全局错误处理程序。有没有办法做到这一点,就像你使用event.stopPropagation()?

实施例

$.get('/something/', function() {
    alert("Stuff went well.");
}).error(function(response)) {
    if (response.status == 404) {
        alert('Page not found');
        // Do something to stop global ajaxError from being called.
    }
});

1 个答案:

答案 0 :(得分:10)

您需要将global: false参数传递给$.ajax,如下所示:

$.ajax({
    url: '/something/',
    global: false,
    success: function() {
       alert('Success');
    }
}).error(function(response)) {
    if (response.status == 404) {
        alert('Page not found');
    }
});

参考:Ajax EventsjQuery.get