Javatpoint Logo
Javatpoint Logo

jQuery ajaxError() method

The ajaxError() method is used to attach a function to be run when an AJAX request fails. It is an AJAX event. jQuery triggers the ajaxError event when an AJAX request completes with an error.

Syntax

The ajaxError() method accepts a single parameter which is defined as follows -

function(event, xhr, options, exc): This is a mandatory parameter. It is a callback function that executes when the request fails.

It also includes some additional parameters named as event, xhr, options, and exc. The additional parameters are defined as follows -

event: It includes the event object.

xhr: It includes the XMLHttpRequest object.

options: It includes the options used in the AJAX request.

exc: It is an exception object. It is passed when an exception occurs during the request processing.

Now, let's see some examples of using the ajaxError() method.

Example1

This is a simple example of using the ajaxError() method. In this example, the function which is passed as the mandatory parameter of the ajaxError() method will be triggered when the request fails. The triggered function will display an alert dialog box showing the error message.

There are two paragraph elements, and we have to click both paragraphs to see the effect. On clicking the first paragraph, a file will load, and the content of the paragraph will get changed. When the user clicks the second paragraph, the request fails because the passed file is missing, so an alert box will be displayed showing the error message.

sample.txt

Example2.html

Test it Now

Output

After the execution of the above code, the output will be -

jQuery ajaxError() method

After clicking the first paragraph with the text Click me, the output will be -

jQuery ajaxError() method

After clicking the second paragraph with the text Click me too, the output will be -

jQuery ajaxError() method

Example2

This example is similar to the first example, except that here we are using the additional parameters of the function passed in the ajaxError() method. Here, we are using the xhr and options parameters to get the URL and status of the request.

Here, there is a paragraph element with text Click me, and we have to click the corresponding paragraph to see the effect. When user clicks the paragraph, the request fails because the passed file is missing, so an alert box will be displayed showing the error message along with the URL and status of the request.

Test it Now

Output

After the execution of the above code, the output will be -

jQuery ajaxError() method

After clicking the paragraph with the text Click me, the output will be -

jQuery ajaxError() method

Above examples are sufficient to show the use of ajaxError() method.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA