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

Open XMLHttpRequest with two arguments - code not working

I can't see what I'm doing wrong here. The task was to add the code to open the AJAX request using GET and pointing to 'footer.html'. I added the line of code below the IF statement, but I get the 'Bummer!' I don't know what else to try.

app.js
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
  if (request.readyState === 4) {
    document.getElementById("footer").innerHTML = request.responseText;
  }
    request.open('GET', 'footer.html');
};
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>AJAX with JavaScript</title>
  <script src="app.js"></script>
</head>
<body>
  <div id="main">
    <h1>AJAX!</h1>
  </div>
  <div id="footer"></div>
</body>
</html>

3 Answers

just move the line outside the function, after the last curly bracket.

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

I was stuck on exact same question. Thayer Y 's solution worked for me.

That was it! Thanks, Thayer Y!