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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources