In jQuery, the ajaxError()
method is used to specify a function that runs when an AJAX request fails.
$(document).ajaxError(function(event, xhr, options, exception));
This method accepts a mandatory function as a parameter. The function accepts the following parameters:
event
: The event objectxhr
: The XMLHttpRequest objectoptions
: The options used in the AJAX requestexception
: The JavaScript exceptionWe 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.