jQuery | ajaxError() Method - GeeksforGeeks

01 Mar, 2019
The ajaxError() method in jQuery is used to specify function to be run when an AJAX request fails.
Syntax:
$(document).ajaxError( function(event, xhr, options, exc) )
Parameter:: This method accepts single parameter function which is mandatory. This function accepts four parameters which are listed below:
event: This parameter holds the event object.
xhr: It holds the XMLHttpRequest object.
options: It contains the used options in AJAX request.
exc: It holds the JavaScript exception.
The demo.txt file stored on server and it will load after clicking the change content button.demo.txt
This is GFG.
Example 1: This example changes the content of <p> element, by taking the data from server. When the AJAX request fails due to an error, the page says AJAX request fails..
<!DOCTYPE html> <html>     <head>         <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">         </script>                   <!-- Script to use ajaxError() method -->        <script>             $(document).ready(function() {                $(document).ajaxError(function() {                    alert("AJAX request fails.");                });                              $("button").click(function() {                    $("#paragraph").load("demo.txt");                });            });        </script>     </head>           <body style="text-align:center;">               <div id="div_content">                       <h1 style = "color: green;">                GeeksforGeeks            </h1>                           <p id = "paragraph" style= "font-size: 20px;">                A computer science portal for geeks            </p>         </div>                   <button>            Change Content        </button>     </body> </html>                    
Output:
Before click on the button:
After click on the button:
Example 2: This example changes the content of <h1> element, by taking the data from server. When the AJAX request fails due to an error, the page says AJAX request fails..
<!DOCTYPE html> <html>     <head>         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">         </script>                   <!-- Script to use ajaxError() method -->        <script>             $(document).ready(function() {                $(document).ajaxError(function() {                    alert("AJAX request fails.");                });                              $("button").click(function() {                    $("#heading").load("demo.txt");                });            });        </script>     </head>           <body style="text-align:center;">               <div id="div_content">                       <h1 id = "heading" style = "color: green;">                GeeksforGeeks            </h1>                           <p style= "font-size: 20px;">                A computer science portal for geeks            </p>         </div>                   <button>            Change Content        </button>     </body> </html>                    
Output:
Before click on the button:
After click on the button:
jQuery-AJAX
Picked
JQuery
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
JQuery | Set the value of an input text field
Form validation using jQuery
How to change selected value of a drop-down list using jQuery?
How to change the background color after clicking the button in JavaScript ?
How to fetch data from JSON file and display in HTML table using jQuery ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?