What is the ajaxError() method in jQuery?

Overview

In jQuery, the ajaxError() method is used to specify a function that runs when an AJAX request fails.

Syntax

$(document).ajaxError(function(event, xhr, options, exception));

Parameters

This method accepts a mandatory function as a parameter. The function accepts the following parameters:

  • event: The event object
  • xhr: The XMLHttpRequest object
  • options: The options used in the AJAX request
  • exception: The JavaScript exception

Example

Console

Explanation

We click the Get Product Data button and make an AJAX request on https://reqres.in/api/products to get product details.

If the AJAX request fails, we invoke the ajaxError() method and the error is logged to the console.

Free Resources