Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.