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

int*_*rgc 4 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.
    }
});
Run Code Online (Sandbox Code Playgroud)

N R*_*ler 10

你需要传递这样的global: falseparam $.ajax:

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

参考:Ajax事件,jQuery.get