jQuery API

.ajaxError()

.ajaxError( handler(event, jqXHR, ajaxSettings, thrownError) ) Returns: jQuery

Description: Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.

  • version added: 1.0.ajaxError( handler(event, jqXHR, ajaxSettings, thrownError) )

    handler(event, jqXHR, ajaxSettings, thrownError)The function to be invoked.

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the .ajaxError() method are executed at this time.

To observe this method in action, set up a basic Ajax load request.

<button class="trigger">Trigger</button>
<div class="result"></div>
<div class="log"></div>

Attach the event handler to any element:

$("div.log").ajaxError(function() {
  $(this).text( "Triggered ajaxError handler." );
});

Now, make an Ajax request using any jQuery method:

$("button.trigger").click(function() {
  $("div.result").load( "ajax/missing.html" );
});

When the user clicks the button and the Ajax request fails, because the requested file is missing, the log message is displayed.

Note: Because .ajaxError() is implemented as a method of jQuery object instances, you can use the this keyword within the callback function to refer to the selected elements.

All ajaxError handlers are invoked, regardless of what Ajax request was completed. To differentiate between the requests, you can use the parameters passed to the handler. Each time an ajaxError handler is executed, it is passed the event object, the jqXHR object (prior to jQuery 1.5, the XHR object), and the settings object that was used in the creation of the request. If the request failed because JavaScript raised an exception, the exception object is passed to the handler as a fourth parameter. For example, to restrict the error callback to only handling events dealing with a particular URL:

$( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) {
  if ( settings.url == "ajax/missing.html" ) {
    $(this).text( "Triggered ajaxError handler." );
  }
});

Example:

Show a message when an Ajax request fails.

$("#msg").ajaxError(function(event, request, settings){
  $(this).append("<li>Error requesting page " + settings.url + "</li>");
});

Support and Contributions

Need help with .ajaxError() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .ajaxError()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • Jared

    How do you unregister an ajax error handler? (I’d like to do this in a success handler.)

    • Lukis

      $(document).unbind('ajaxError');

    • Drew

      If this question was so that a global error handler does not fire, it's best to put something like this into the global error handler instead: if(!ajaxOptions.error) or if(!ajaxOptions.success) where ajaxOptions are passed into the global error handler as the third variable. Good luck!

  • Andreas Krüger

    That's a simple (quick and dirty) way to check on errors in ajax requests via alert.
    $(document).ajaxError(function(e, xhr, settings, exception) {
    alert('error in: ' + settings.url + ' \n'+'error:\n' + exception);
    });

  • http://randomtweak.com randomtweak

    is there anyway to retrieve the HTTP response code? An error message for 404 might be different to one for a 500, for example.

    • http://twitter.com/neil_big_craig Neil Craig

      The XMLHttpRequest should have a status property that will have the code you’re looking for

    • http://twitter.com/acdameli craig d'amelio

      Yeah, this is an old question but…

      jQuery.ajax({
      …,
      complete: function(e, XHR, options) {
      if (XHR.status == 200) { // success
      } elseif (XHR.status == 500) { // Internal Server Error
      } elseif (XHR.status == 404) { // File Not found
      }
      });

      more response codes here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec…

      Also note: It has been my experience that the 3XX level of codes indicating redirection are followed by the request automatically so you can't catch them here. At least I could not catch 301 (Moved Permanently) or 302 (Found).

    • Theron

      try echoing the responseText attribute of the request object.it should have the servers default web page error message for that error. as in :

      $(document).ajaxError(function(e, xhr, settings, exception) {
      alert('error in: ' + settings.url + ' n'+'error:n' + xhr.responseText );
      });

      • Grant

        responseText would be from the HTTP response body, AFAIK, and not all error responses will have a body. I think there's a statusText property of XHR, though.

  • http://itcomputerzone.com steve

    the requested page can return 404 or 403 error codes
    but the errorThrown variable is ALWAYS undef…
    any idea ??

  • http://www.twitter.com/NicBright Nicolas B.

    It'd be nice if there was an example of the possible content of “thrownError”.

    • Theron

      if you want to actually get the response from the called page even on error call the responseText attribute of the XMLHttpRequest object as in :

      $(document).ajaxError(function(e, xhr, settings, exception) {
      alert('error in: ' + settings.url + ' n'+'error:n' + xhr.responseText );
      });

    • Grantwparks

      ” If the request failed because JavaScript raised an exception, the exception object is passed to the handler as a fourth parameter”

      I think the best bet is looking at Javascript Exception class. And then examples would be dependent on context.

      • D851

        I still agree with with Nicolas B. An example is worth so much more than mere words, especially when you're not that familliar with the subject

  • Aa

    How can I know if ajaxError was called after ajax timeout?

    • Arly Rampen

      try making the server-side request to sleep (using sleep() for PHP) for more than the ajax timeout you've set, it should trigger the ajaxError due to delayed response.

  • Pandurang S. Parwatikar

    responseText is null in some cases

  • Arly Rampen

    try making the server-side request to sleep (using sleep() for PHP) for more than the ajax timeout you've set, it should trigger the ajaxError due to delayed response.

  • SparK

    why $.post can't simply throw an exception, if noone catches it it will remain silent as always…

  • Dennis C

    Anyone know a way to use this handler without having to attaching it to an element?

    • http://twitter.com/leZgal Almino Melo

      I tried “$(document).ajaxError(…);” and it worked.