JavaScript AJAX Basics (retiring) Programming AJAX Parsing JSON Data

Susan Rusie
Susan Rusie
10,670 Points

What is missing out of my code?

If I run this program and check the console, I get two messages: 1) XLMHttpRequest cannot load (widget.js:9) and 2) Uncaught SyntaxError: unexpected end of JSON input (index.html:1). It looks like something may be wrong with send, but I am not sure what. I never get see the list of employees like Dave shows in the video. Any help on this would be greatly appreciated. Here is my code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>AJAX Office Status Widget</title>
  <link href='http://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="css/main.css">
 <script src="js/widget.js"></script> 
</head>
<body>
  <div class="grid-container centered">
    <div class="grid-100">
      <div class="contained">
        <div class="grid-100">
          <div class="heading">
            <h1>Corporate Intranet</h1>
          </div>
        </div>
        <section class="grid-70 main">
          <h2>Lorem Ipsum Headline</h2>
          <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
          <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
          <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
        </section>
        <aside class="grid-30 list">
          <h2>Employee Office Status</h2>
          <div id="employeeList">

          </div>
        </aside>
        <footer class="grid-100">
          <p>Copyright, The Intranet Corporation</p>
        </footer>
      </div>
    </div>
  </div>
</body>
</html>
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if(xhr.readyState === 4){
        var employees = JSON.parse(xhr.responseText);
        console.log(employees);
    }
};  
xhr.open('GET', 'data/employees.json');
xhr.send(); 

2 Answers

Kai Detmers
Kai Detmers
9,571 Points

Have you saved the document? Maybe the changes havent taken effect and thats why its not working. I tried to paste in the code of you & its working for me.

ywang04
ywang04
6,733 Points

Yes, the code provided by Susan is also working for me on my local environment.

Susan, it looks to me like you are missing checking for the xhr.status. Try something along these lines:

var xhr = new XMLHttpRequest();
var url = "data/employees.json";

xhr.onreadystatechange = function() {
    if(xhr.readyState === 4 && xhr.status == 200) {  //need both readyState and status
        var employees = JSON.parse(xhr.responseText);
        console.log(employees);
    }
};  
xhr.open('GET', url, true);
xhr.send(); 
Susan Rusie
Susan Rusie
10,670 Points

I made the changes you suggested and it still is not working. I am running it locally through Notepad++ using XAMPP and when I open the developer tools, I get this message: "XMLHttpRequest cannot load". It lists the project file folder I have this work saved in and then goes on to say this: "Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource." At the bottom, it says "(anonymous function) @ widget.js: 10", which is pointing to a problem with send. Is there anything else I could try?