12

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?

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

}

1 Comment

Actually you'd want the boolean in the global ajax error handler, and you would set it in the local ajax call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.