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
liam johnshon
Courses Plus Student 904 PointsAJAX request
hey i m working on a second challenge given by Dave in ajax section the code is working fine but it show's this error "Uncaught SyntaxError: Unexpected end of input" two times. what is it ?
liam johnshon
Courses Plus Student 904 Points//making an first ajax request
var ajx = new XMLHttpRequest();
//callback
ajx.onreadystatechange = function(){
//if all the conditions is OK than go for statements under conditions
if(ajx.readyState===4 && ajx.status===200){
//creating HTML element required for server response
var ul = '<ul class="bulleted">';
//capturing the json string and convert it into javascript form
var employees = JSON.parse(ajx.responseText);
//looping through each employee
for(var i=0;i<employees.length;i++){
//making if else condition to check whether the employee is in or out
if(employees[i].inoffice===false){
//adding li as a children of ul
ul+='<li class="in">'+employees[i].name+'</li>';
}else{
ul+='<li class="out">'+employees[i].name+'</li>';
}
}
//closing ul tag
ul+='</ul>';
//now we need to put the following ul in html structure
document.getElementById("employeeList").innerHTML = ul;
}
};
ajx.open("GET","../data/employees.json");
ajx.send();
//making an new ajax request
var ajax = new XMLHttpRequest();
//callback option
ajax.onreadystatechange = function(){
//creating an html element
var ul = '<ul class="rooms">';
//converting json into javascript
var rooms = JSON.parse(ajax.responseText);
//looping through each room
for(var i=0;i<rooms.length;i++){
//using if condition to check is room available is false (full)or true(empty)
if(rooms[i].available===true){
ul+='<li class="empty">'+rooms[i].room+'</li>';
}else{
ul+='<li class="full">'+rooms[i].room+'</li>';
}
}
//closing ul tag
ul+='</ul>';
//updating HTML request
document.getElementById("roomList").innerHTML = ul;
};
ajax.open("GET","../data/rooms.json");
ajax.send();
1 Answer
Steven Parker
243,656 PointsIn the first request ("ajx"), the provided sample code performs this test first in the onreadystatechange method:
//if all the conditions is OK than go for statements under conditions
if(ajx.readyState===4 && ajx.status===200){
But in the second request ("ajax"), you forgot to perform this test, so the parser is invoked even when there is no valid responseText, causing the error.
This would have been a good place to post a link to a workspace snapshot.
António Rodrigues
13,743 PointsAntónio Rodrigues
13,743 PointsHello Liam, could you post the code.