Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
67,621 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?