jQuery ajax - ajaxError() method

Example

Trigger a prompt box when the AJAX request fails:

$("div").ajaxError(function(){
  alert("An error occurred!");
});

Try It Yourself

Definition and Usage

The ajaxError() method executes a function when an AJAX request occurs an error. It is an Ajax event.

Syntax

.ajaxError(function(event,xhr,options,exc))
Parameters Description
function(event,xhr,options,exc)

Required. Specifies the function to be executed when the request fails.

Additional Parameters:

  • event - Contains event object
  • xhr - Contains XMLHttpRequest object
  • options - Contains options used in the AJAX request
  • exc - Contains JavaScript exception

Detailed Description

The XMLHttpRequest object and settings are passed to the callback function as parameters. The caught error can be passed as the last parameter:

function (event, XMLHttpRequest, ajaxOptions, thrownError) {
 // thrownError will be passed to this only when an exception occurs;
 }

More Examples

using xhr and options Parameters
How to Use options parameters to obtain more useful error messages.