0

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?

1 Answer 1

2

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/

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

4 Comments

I've been reading about it, but what is this div.log? I'm assuming I can just create the div there on the fly $('<div class="log" />').ajaxError(function () {}); right? I just want everything to be logged mainly
You may create <div class="log" /> or just use $.ajaxError is OK no problem
That's awesome! I'll play around with this and add my events in there then. Any idea if having error: function () { } on a page automatically overwrites .ajaxError()?
You can have additional error: somewhere else :), It will not overwrite but call both of them

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.