Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 66971

Handling jQuery AJAX Errors globally

What are your guys thoughts on globally handling AJAX errors, displaying any/all info in the console?

As of right now I do something like this:

jSpace._displayAjaxErrors = function (xhrResponseText) {

    var errObj = $.parseJSON(xhrResponseText);

    // _log is an internal console log that won't break in IE (aka does nothing)
    _self._log('\n -- AJAX ERROR @ ' + _self._getTimeStamp() + ' --');

    _self._log('ExceptionType : '   + errObj.ExceptionType);
    _self._log('Error Message : '   + errObj.Message);
    _self._log('StackTrace    : \n' + errObj.StackTrace);​

    // then a dialogBox here
};

And each .ajax() has this included at the moment:

error: function (xhr, status, error) {
    jSpace._displayAjaxErrors(xhr.responseText);
}

I'm not familiar with .ajaxError(), but wondering if there is a better way of doing all of this!

What do you do to easily see what/where the errors are?

Upvotes: 0

Views: 692

Answers (1)

Nhu Trinh
Nhu Trinh

Reputation: 13956

jQuery already provide ajaxError function to catch all ajax error globally, you just need to use that

$( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) {
  //e for event 
  //jqxhr for xhrResponse
});

http://api.jquery.com/category/ajax/global-ajax-event-handlers/

Upvotes: 2

Related Questions