Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

jQuery: Register a handler to be called when Ajax requests complete with an error

jQuery Fundamental - I : Exercise-12

Register a handler to be called when Ajax requests complete with an error.

Sample solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
 <meta name="viewport" content="width=device-width">
 <title>Register a handler to be called when Ajax requests complete with an error.</title>
 </head>
 <body>
 <button class="trigger">Click to see the file content</button>
 <div class="result"></div>
 <p class="log"></p>
 </body>
 </html>

JavaScript Code :

$( document ).ajaxError(function() {
  $( ".log" ).text( "File not present." );
});

$( "button.trigger" ).on( "click", function() {
  $( ".result" ).load( "https://www.w3resource.com/jquery-exercises/jq-ajax-test.php" );
});

Used Methods :

  • .ajaxError() : Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.

See the Pen jquery-fundamental-exercise-12 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Register a handler to be called when Ajax requests complete.
Next: Attach a function to be executed before an Ajax request is sent.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.