最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: How to stop multiple jQuery ajax error handlers? - Stack Overflow

programmeradmin1浏览0评论

A team member put this into our project

$(function() {
    $("body").bind("ajaxError", function(event, XMLHttpRequest, ajaxOptions, thrownError){
        alert(thrownError);
    });
}

However I want to supress one of my errors (as it would be noise and verification isn't needed)

function blah() {

    ...

    errFunc = function(event, xhr, opts, errThrown) {
        //what could I do here?
        //event.stopImmediatePropagation()?
    }

    $.ajax({
        url        : '/unimportant_background_refresh',
        type       : 'GET',
        data       : { },
        dataType   : 'json',
        success    : updateBackgroundStuff,
        error      : errFunc,  // Suppresses error message.
    });

}

How can I stop the catch all error from happenning please? Can I just do something in the error function such as { event.StopPropogation(); } or must I work out some mechanism for having the catch all selectively ignore things please?

A team member put this into our project

$(function() {
    $("body").bind("ajaxError", function(event, XMLHttpRequest, ajaxOptions, thrownError){
        alert(thrownError);
    });
}

However I want to supress one of my errors (as it would be noise and verification isn't needed)

function blah() {

    ...

    errFunc = function(event, xhr, opts, errThrown) {
        //what could I do here?
        //event.stopImmediatePropagation()?
    }

    $.ajax({
        url        : '/unimportant_background_refresh',
        type       : 'GET',
        data       : { },
        dataType   : 'json',
        success    : updateBackgroundStuff,
        error      : errFunc,  // Suppresses error message.
    });

}

How can I stop the catch all error from happenning please? Can I just do something in the error function such as { event.StopPropogation(); } or must I work out some mechanism for having the catch all selectively ignore things please?

Share Improve this question asked Nov 8, 2011 at 13:52 PhilluminatiPhilluminati 2,7892 gold badges26 silver badges34 bronze badges 1
  • 1 stackoverflow.com/questions/7436195/… – aziz punjani Commented Nov 8, 2011 at 14:05
Add a comment  | 

2 Answers 2

Reset to default 19

Global events can be disabled, for a particular Ajax request, by passing in the global option, like so:

 $.ajax({
   url: "test.html",
   global: false,
   // ...
 });

Taken from: http://docs.jquery.com/Ajax_Events

I would just throw a boolean into your code that's defaulted to false. Set it to true the first time the error is thrown and make sure to check for true at the start of the error function.

Something like:

function blah() {
    var errorHappened = false;

    ...

    errFunc = function(event, xhr, opts, errThrown) {
        if (errorHappened)
            return;

         errorHappened = true;

         // handle the errror.

    }

    // the rest of your code

}
发布评论

评论列表(0)

  1. 暂无评论