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) 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.

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~~