JavaScript AJAX Basics (retiring) Programming AJAX Processing JSON Data

Pieter Van Looy
Pieter Van Looy
7,762 Points

My code doesn't work

According to me, I have the same code as in the tutorial. Nevertheless, I get an syntax error message. What is wrong with my code?

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if(xhr.readyState === 4) {
    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();

Edited for readabilty (Please review the Markdown Cheatsheet to see how to properly show code) - Dane E. Parchment Jr. (Moderator)

3 Answers

Shlomi Bittan
Shlomi Bittan
6,717 Points

Hi, Dane is right, the syntax error is from that comma. But i noticed another problem in your code that will cause it to fail. This line is wrong:

statusHTML += '<ul>';

You suppose to close the ul, so do this instead:

statusHTML += '</ul>';
Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 9,733 Points

Well your syntax error is in your loop. Your first parameters are being separated by a comma instead of a semicolon. Is that the syntax error you are referring to?

Pieter Van Looy
Pieter Van Looy
7,762 Points

Hi guys or girls ;-),

that seems to be it. Thanks a lot!