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

Cesar Guerrero
Cesar Guerrero
11,190 Points

New to AJAX

Why isn't this working? I am using the correct functions and using the right methods to retrieve the "footer.html"

3 Answers

Juan P. Prado
Juan P. Prado
69,294 Points

You need the open methond of the request object outside of the callback function as follows, and lastly you'll call the send method, we assign a callback function to the event of onreadystatechange, so that when the state changes and it happens to be done, we'll run some code. In the open method we especify what we are going to pass over the http protocol, we'll pass 2 parameters, the http method, an the path or resource. Hope this helps.

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

Post the code you are using? (remember to enclose in three backticks to enable code highlighting)

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

I think you are missing one crucial step in the 4 steps listed in the video, yes?