JavaScript AJAX Basics (retiring) Programming AJAX Processing JSON Data

Chisel Delahanty
Chisel Delahanty
3,609 Points

Finished project files aren't working for me

The employee list is not appearing for me, even when I use the finished project files. I get the following console error message:

Uncaught TypeError: Cannot read property 'inoffice' of undefined at XMLHttpRequest.xhr.onreadystatechange (widget.js:7)

Does anyone have any advice? My full code looks like this:

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; employees.length; i++) {
      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();
Steven Parker
Steven Parker
203,137 Points

It might help to see the whole workspace for this one. You could make a snapshot of your workspace and post the link to it here.

Chisel Delahanty
Chisel Delahanty
3,609 Points

Never mind, I found the problem myself – a missing "i <" in my for loop syntax. D'oh!

I'm glad to have gotten the tip about how to make a snapshot of the workspace, though – I hadn't known that. Thanks Steven!

1 Answer

Steven Parker
Steven Parker
203,137 Points

Congratulations on resolving your issue! :+1: