ajaxError() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:ajaxError

Description

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

Syntax

Parameter Description
function(event,xhr,options,exc) Required.

Additional parameters:

  • event - the event object
  • xhr - the XMLHttpRequest object
  • options - the options used in the AJAX request
  • exc - the JavaScript exception, if one occurred

The following code shows how to Trigger an alert box when an AJAX request fails:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(document).ajaxError(function(){
        console.log("An error occured!");
    });/*from  w  w  w.j  a va 2 s  . c o  m*/
    $("button").click(function(){
        $("div").load("wrongfile.txt");
    });
});
</script>
</head>
<body>

<div><h2>Let AJAX change this text</h2></div>

<button>Change Content</button>

</body>
</html>

Related Tutorials