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

Matthew Clark
Matthew Clark
7,255 Points

My code is incorrect here? Help!

What is the issue with my code here?

$.get("missing.html", function(data) {
  $("#footer").html(data);
}).fail(function (jqXHR){
$('#footer').statusText(errorMessage);
});
Ken Alger
Ken Alger
Treehouse Teacher

Edited for markdown.

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Matthew;

You are close, but let's take a look at this...

Task 3 of this code challenge is:

When the AJAX response fails, we should alert the user. Pop up an alert box with the XHR object's statusText property.

Using the 4 P's of problem solving (Preparation, Plan, Perform, Perfect), and we'll probably at most use three here, let's examine this problem.

Preparation

As we have gotten through the first two tasks we should be starting our Task 3 with code similar to:

$.get("missing.html", function(data) {
  $("#footer").html(data);
}).fail(function(jqXHR){});

Plan

The challenge asks us to

  1. Pop up an alert box. Great! There is an alert() method for that.
  2. ... with the XHR object's statusText property. Since our XHR object is named jqXHR, we need to use jqXHR.statusText.

Perform

So, using the info we have, our code will look like:

// Course:  AJAX Basics
// Module: jQuery & AJAX
// Challenge: Handle AJAX failures with jQuery
// Task: 3 of 3

$.get("missing.html", function(data) {
  $("#footer").html(data);
}).fail(function(jqXHR){
  alert(jqXHR.statusText);
});

That not only does what the challenge asks and is direct to-the-point code, it also passes the challenge! Cool. What's next?

Happy coding,

Ken

San Francisco
San Francisco
28,373 Points

Bless you for providing clear, concise, workable solutions for these problems. If only all answers were like this lol.