JavaScript AJAX Basics (retiring) jQuery and AJAX Handle AJAX failures with jQuery

Ronnie Barua
Ronnie Barua
17,665 Points

Can't solve this jQuery's $.get() method doesn't do anything if the server returns an error. You can chain jQuery's fai

jQuery's $.get() method doesn't do anything if the server returns an error. You can chain jQuery's fail() method to the $.get() method and pass it a function to handle any errors. Start by adding the fail() method to the $.get() method.

I have added at the end- .fail(function()

app.js
$.get("missing.html", function(data) {
  $("#footer").html(data);
}).fail(function();
       );
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>AJAX with JavaScript</title>
</head>
<body>
  <div id="main">
    <h1>AJAX with jQuery</h1>
  </div>
  <div id="footer"></div>
  <script src="jquery.js"></script>
  <script src="app.js"></script>
</body>
</html>

2 Answers

Hugo Paz
Hugo Paz
15,607 Points

Hi Ronnie,

You just need to add the fail method to it first,

$.get("missing.html", function(data) {
  $("#footer").html(data);
}).fail();
Ronnie Barua
Ronnie Barua
17,665 Points

Thanks a lot Hugo. I thought of that but I didn't think that's all it wants.