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

conditional statement you just created, also test to make sure the return status from the server is OK.

Please help . Can you see what I am doing wrong.

app.js
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
  };
};
document.getElementById('sidebar').innerHTML = xhr.responseTest;
} else {
  alert(xhr.statusTest);
}};
xhr.open('GET', 'sidebar.html');
xhr.send();
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="sidebar"></div>
</body>
</html>

6 Answers

This is what Dave is looking for...

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

You can also do this:

app.js
var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4){  // 4 means the readystate has send everything that it will send
      if (xhr.status === 200){ // can use if (xhr.readyState === 4, xhr.status === 200)
        document.getElementById('sidebar').innerHTML = xhr.responseText;
      }
    }
};
xhr.open('GET', 'sidebar.html');
xhr.send();
Adam Beer
Adam Beer
11,314 Points

This is the first task solution. xhr.status === 200 means that everything is good. Hope this help.

if (xhr.readyState === 4) {
    //In the conditional statement you just created, also test to make sure the return status from the server is OK.
  }

Yes, I recall that in the video but I don't know why it does not pass to the next task. If I understand the question correctly it wanted to check the status if it is OK.

Adam Beer
Adam Beer
11,314 Points

This is working for me. Please try this step by step. Use document.getElementById() inside the if statement and don't use else statement, please corrected and delete it. Hope this help.

if(xhr.readyState === 4) {
     if(xhr.status === 200) {

     }
  }

Yes, I recall that in the video but I don't know why it does not pass to the next task. If I understand the question correctly it wanted to check the status if it is OK.

i know i am three years late to the party but i just wanted to state that i used the following code and it also worked

var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { return xhr.responseText;

}

}; xhr.open('GET', 'sidebar.html'); xhr.send();

You missed it in part because of a typo! It's responseText, not responseTest!