jQuery ajaxError()

Introduction

Trigger an alert box when an AJAX request fails:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>// ww w.  j a  va  2 s .  co  m
<script>
$(document).ready(function(){
  $(document).ajaxError(function(){
      document.getElementById("demo").innerHTML = "An error occured!";
  });
  $("button").click(function(){
      $("div").load("wrongfile.txt");
  });
});
</script>
</head>
<body>

<p id="demo"></p>

<div><h2>Here</h2></div>

<button>Change Content</button>

</body>
</html>

The ajaxError() method sets a function to run when an AJAX request fails.

This method should only be attached to document.

$(document).ajaxError(
     function(event,xhr,options,exc)
);
Parameter
Optional
Description
function(event,xhr,options,exc)





Required.





function to run if the request fails
Additional parameters:
event - contains the event object
xhr - contains the XMLHttpRequest object
options - contains the options used in the AJAX request
exc - contains the JavaScript exception, if one occurred



PreviousNext

Related