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 trialKaren Shumate
13,579 Pointsconditional 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.
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();
<!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
John Norris
21,145 PointsThis 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
33,500 PointsYou can also do this:
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
11,314 PointsThis 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.
}
Karen Shumate
13,579 PointsYes, 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
11,314 PointsThis 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) {
}
}
Karen Shumate
13,579 PointsYes, 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.
Yeukai Patience Shiripinda
8,067 Pointsi 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();
Ross Williams
Front End Web Development Techdegree Graduate 15,516 PointsYou missed it in part because of a typo! It's responseText, not responseTest!