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 trialanguswhiston
17,225 PointsCan't get the meeting rooms to show up AJAX Stage 2 JSON
I have copied it exactly as was put in the lesson, I have launched the workspace from the same lesson as well. Even when copied exactly i can't get the rooms to appear... Am I doing something wrong, do we need to edit the .css?
var roomRequest = new XMLHttpRequest();
roomRequest.onreadystatechange = function () {
if(roomRequest.readyState === 4) {
var rooms = JSON.parse(roomRequest.responseText);
var statusHTML = '<ul class="rooms">';
for (var i=0; i<rooms.length; i += 1) {
if (rooms[i].available === true) {
statusHTML += '<li class="empty">';
} else {
statusHTML += '<li class="full">';
}
statusHTML += rooms[i].room;
statusHTML += '</li>';
}
statusRoom += '</ul>';
document.getElementById('roomList').innerHTML = statusHTML;
}
};
roomRequest.open('GET', '../data/rooms.json');
roomRequest.send();
3 Answers
Steven Byington
13,584 PointsHere is what I have that works. The only difference I see is you aren't checking if status === 2000.
var room = new XMLHttpRequest();
room.onreadystatechange = function () {
if(room.readyState === 4 && room.status === 200) {
var rooms = JSON.parse(room.responseText);
var statusHTML = '<ul class="rooms">';
for (var i=0; i<rooms.length; i += 1) {
if (rooms[i].available === true) {
statusHTML += '<li class="empty">';
} else {
statusHTML += '<li class="full">';
}
statusHTML += rooms[i].room;
statusHTML += '</li>';
}
statusHTML += '</ul>';
document.getElementById('roomList').innerHTML = statusHTML;
}
};
room.open('GET', '../data/rooms.json');
room.send();
Amy Herbertson
3,144 PointsThis looks similar and I can't figure out why it doesn't work!
var roomRequest = new XMLHttpRequest();
roomRequest.onreadystatechange = function () {
if(roomRequest.readyState === 4) {
var rooms = JSON.parse(roomRequest.responseText);
var statusHTML = '<ul class="rooms">';
for (var i=0; i<rooms.length; i += 1) {
if (rooms[i].available === true) {
statusHTML += '<li class="empty">';
} else {
statusHTML += '<li class="full">';
}
statusHTML += rooms[i].room;
statusHTML += '</li>';
}
statusHTML += '</ul>';
document.getElementById('roomList').innerHTML = statusHTML;
}
};
roomRequest.open('GET', '../data/rooms.json');
roomRequest.send();
Juan Miguel Corona
5,321 PointsHi Amy, the only difference I see is that your first conditional statement doesn't check that roomRequest status is 200. Hope that helps!
Vincent Dubay
6,220 PointsHey there! If you have this in the same javascript file as the request that fetches the employee data, it's the statusHTML variable that might be causing the issue. The first request uses statusHTML to combine the employee status HTML, and if used again in the request for the room data it adds all the previous markup.
Here's what worked for me:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4 && xhr.status === 200) {
var employees = JSON.parse(xhr.responseText);
var statusHTML = '<ul class="bulleted">';
for (var i=0; i<employees.length; i += 1) {
if (employees[i].inoffice === true) {
statusHTML += '<li class="in">';
} else {
statusHTML += '<li class="out">';
}
statusHTML += employees[i].name;
statusHTML += '</li>';
}
statusHTML += '</ul>';
document.getElementById('employeeList').innerHTML = statusHTML;
}
};
xhr.open('GET', 'data/employees.json');
xhr.send();
var XHRrooms = new XMLHttpRequest();
XHRrooms.onreadystatechange = function() {
if(XHRrooms.readyState === 4) {
var rooms = JSON.parse(XHRrooms.responseText);
var roomHTML = '<ul class="rooms">';
for (var i=0; i < rooms.length; i += 1) {
if (rooms[i].available === true) {
roomHTML += '<li class="empty">';
} else {
roomHTML += '<li class="full">';
}
roomHTML += rooms[i].room;
roomHTML += '</li>';
}
roomHTML += '</ul>';
document.getElementById('roomList').innerHTML = roomHTML;
}
};
XHRrooms.open('GET', 'data/rooms.json');
XHRrooms.send();
Cheers!