Home >>jQuery Tutorial >jQuery ajaxError() Method

jQuery ajaxError() Method

jQuery ajaxError() Method

jQuery ajaxError() method in jQuery is used to specifies a function to be run when an AJAX request fails.

Syntax:
$(document).ajaxError(function(event,xhr,options,exc))

Parameter Values

Parameter Description
function(event,xhr,options,exc) It is a Required parameter and is used to specifies the function to run if the request fails.
Additional parameters:
  • event – It is used to contains the event object
  • xhr – It is used to contains the XMLHttpRequest object
  • options - It is used to contains the options used in the AJAX request
  • exc – It is used to contains the JavaScript exception, if one occurred
Here is an Example of jQuery ajaxError() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).ajaxError(function(){
alert("Occured an Error!");
});
$(".btn1").click(function(){
$(".div1").load("wrongfile.txt");
});
});
</script>
</head>
<body>
<div class="div1"><h2>Change this text</h2></div>
<button class="btn1">Click me to Change Content</button>
</body>
</html>

Output:

Change this text