CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: JavaScript

$.ajax() error handler

489 Views
Copy Code Show/Hide Line Numbers
error: function(xhr, status) {                   
    var err = null;
    if (xhr.readyState == 4) {
        var res = xhr.responseText;
 
        if (res && res.substr(0,1) == '{')
            var err = JSON.parseWithDate(res);
        if (!err) {
            if (xhr.status && xhr.status != 200)
                err = new CallbackException(xhr.status + " " + xhr.statusText);
            else
                err = new CallbackException("Callback Error: " + status);
            err.detail = res;
        }
    }
    if (!err)
        err = new CallbackException("Callback Error: " + status);
 
    if (errorHandler)
        errorHandler(err, _I, xhr);
}
by Rick Strahl
  September 11, 2009 @ 6:43pm
Tags:
Description:
Getting a consistent error message from failures is a pain. XHR isn't fully available (causing all sorts of weird errors if readyState != 4 trying to access .status or responseText) when the request isn't complete yet for errors like timeout. If the server returned a JSON message great. Otherwise - not so much.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate