English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery ajaxError() Method

jQuery AJAX Methods

When an Ajax request is completed and an error occurs, the ajaxError() method will attach a function to be called. This is an AjaxEvent.

jQuery will trigger the ajaxError event every time an Ajax request is completed and an error occurs. At this time, all handlers registered with the ajaxError() method will be executed.

Note:From jQuery 1.8Since version, this method should only be attached to the document.

Syntax:

$("document").ajaxError(function(event, xhr, options, thrownError))

Example

Display a message when an Ajax request fails:

$("document").ajaxError(function(){
  $("#error").text("An error occurred!!!");
});
Test to see‹/›

You can get more useful output by using the event, xhr, and options parameters:

$("document").ajaxError(function(event, xhr, options){
  $("#error").append("Error requesting page: "); + options.url);
  $("#error").append(xhr.status);
  $("#error").append(event.type);
});
Test to see‹/›

Parameter Value

ParametersDescription
function(event, xhr, options, thrownError)Specify a function to run when the request completes and an error occurs

Parameters:

  • event -Contains event object

  • xhr-Contains XMLHttpRequest object

  • options-Contains options used in AJAX request

  • thrownError-Contains JavaScript exceptions (if any)

jQuery AJAX Methods