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.



Follow us on Facebook and Twitter for latest update.