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) AJAX Concepts Finish the AJAX Request

Nicholas Arcuri
Nicholas Arcuri
11,311 Points

"Add the code to open the AJAX request using the GET method and pointing to the 'footer.html' file." Answer not working

Unless I'm missing something, it's not accepting my answer:

var request = new XMLHttpRequest();
request.onreadystatechange = function () {
  if (request.readyState === 4) {
    document.getElementById("footer").innerHTML = request.responseText;
  }
  request.open('GET', 'footer.html');
};

4 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Nicholas Arcuri

You have the request.open('GET', 'footer.html'); inside the onreadystatechange callback. It has to be outside of that (after the last closing brace.)

The onreadystatechange callback only ever runs AFTER the .open() and .send() methods are executed.

Nicholas Arcuri
Nicholas Arcuri
11,311 Points

Oh wow, I had a feeling I was doing something dumb. Thanks Dave!

more specific:

request.open('GET', 'footer.html', true);

UPDATE:

ok try to add status to if statement and in request.open use double quotes and add the two last lines after open like below:

if (request.readyState === 4  && request.status === 200) {
    document.getElementById("footer").innerHTML = request.responseText;
  }
  request.open("GET", "footer.html", true);
request.onreadystatechange = handleServerResponse;
request.send();

Hi Nicholas,

Not sure, but I think you may need to add a third argument in your call to open() for the async value: true for asynchronous, false for synchronous.

-Agapito

Nicholas Arcuri
Nicholas Arcuri
11,311 Points

Nah that's not doing it. A bug maybe?