JavaScript AJAX Basics (retiring) Programming AJAX Processing JSON Data

Code won't show correctly in the browser.

After typing all the code, and viewing in the browser, content won't show the way it should in the browser, am I missing something?

const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
   if(xhr.readystate === 4) {
       const employees = JSON.parse(xhr.responseText);
       const statusHTML = '<ul class="bulleted">';
       for(const 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;
};'GET', 'data/employees.json');

Edited By Dane E. Parchment Jr. (Moderator) - P.S. Please make sure you use code tags on your code examples to make them more readable.

1 Answer

Antti Lylander
Antti Lylander
9,668 Points

I believe you cannot use const in a for loop like you have. Don't know whether you have any other problems there. It would be easier to find the error if you posted the whole workspace as a snapshot.

statusHTML needs to be declared with let also