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

JavaScript AJAX Basics (retiring) Programming AJAX Parsing JSON Data

Muhammad Haris Khan
Muhammad Haris Khan
8,587 Points

Getting GET error when i send request

I am getting error in my console :

GET http://port-80-z63kj2b4h4.treehouse-app.com/widget.js

I do not know what is wrong with the following code:

var xhr = new XMLHttpRequest;

var onreadystatechange = function(){
  if(xhr.readyState === 4){
    var employees = JSON.parse(xhr.responseText);
    console.log(typeof employees);
  }
};


xhr.open('GET', 'data/employees.json');
xhr.send();

3 Answers

Joseph Perez
Joseph Perez
25,122 Points

Maybe try

var xhr = new XMLHttpRequest();

Instead of what you had before which was

var xhr = new XMLHttpRequest;
Daniel Jenkins
Daniel Jenkins
17,714 Points

As well as the change suggested above you'll also need to change the following line:

var onreadystatechange = function(){

to

xhr.onreadystatechange = function(){

This is because

onreadystatechange

is a method of the xhr object and shouldn't be declared as a variable

I found 2 problems in your code.

  1. No Parenthesis after XMLHttpRequest. It should be var xhr = new XMLHttpRequest();
  2. You cannot have a language reserve name as a variable, and it should be like, xhr.onreadychangestate = function (){ //Body };