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) Programming AJAX Check for the correct ready state

yitan
yitan
8,640 Points

Now use a DOM element's innerHTML property to set the HTML inside the sidebar div to the server's response.

I typed as follows: document.getElementById('sidebar').innerHTML = xhr.responseText; But it can not pass the check...

I'm having the same issue. I went through my notes and previous Workspace and it shows this example and how it needs to be implemented which is exactly what I'm typing in for the code challenge. Any other suggestions.

 xhr.onreadystatechange = function () {
   if (xhr.readyState === 4 && xhr.status === 200){
    document.getElementById('sidebar').innerHTML = xhr.responseText;
    }
    };

8 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Good catch. There is a problem with the Code Challenge. We're working to get that fixed. Sorry for the inconvenience.

Here is my solution:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if(xhr.readyState === 4) {
    if(xhr.status === 200) {
      document.getElementById('sidebar').innerHTML = xhr.responseText;
    }
  }
};
xhr.open('GET', 'sidebar.html');
xhr.send();
Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi yitan and Luca Terrigno

That code challenge is now fixed. Thanks for the heads up on it.

Alessandro Zerillo
Alessandro Zerillo
7,379 Points

i think there is still a problem

Dave McFarland
Dave McFarland
Treehouse Teacher

Alessandro Zerillo can you post the code that you're using in the code challenge?

Hi,

I'm having the same issue as above, here is my code that works but the test isn't passing.

var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { let sidebar = document.getElementById('sidebar'); console.log(xhr.responseText); sidebar.innerHTML = xhr.responseText; } }; xhr.open('GET', 'sidebar.html'); xhr.send();

Tommy Choe
Tommy Choe
38,156 Points

I tried copying and pasting your code directly into the challenge and it seems to work just fine for me. Check to see if you added any extra characters somewhere else.

John Norris
John Norris
21,145 Points

Here's my solution:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
          if (xhr.readyState === 4, xhr.status === 200) {
        document.getElementById('sidebar').innerHTML =xhr.responseText;
      }
};
xhr.open('GET', 'sidebar.html');
xhr.send();

Dave, Do u mind to check again cause i can't past the test with the same problem

Dave McFarland
Dave McFarland
Treehouse Teacher

Can you post the code that you are trying?

if(xhr.readyState === 4 && xhr.status === 200) {
   document.getElementById('sidebar').innerHTML = xhr.responeText;
}
Dave McFarland
Dave McFarland
Treehouse Teacher

Here's the problem Patrick: xhr.responeText

Is should be response not respone

opps...... didn't found out the problem~~ sorry for disturb and thank for the solution~~