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

Why is my AJAX not working?

I have a file folder set up to work with AJAX. I have a dummy json file:

{
    "tweets": [
        {
          "content": "hello"  
        },
        {
          "content": "world"  
        }
    ]
}
window.load = function() {   

    var http = new XMLHttpRequest();


    http.onreadystatechange = function() {
        console.log(http);
    }

    http.open('GET', 'data/tweets.json', true);
    http.send();

}

The problem I'm having, is that when I console.log the http object, I keep getting 4 XMLHttpRequest objects - all of which, have a readystate of 4. But, I believe I'm supposed to get different ready states. This makes me think my code is wrong.

However, if I change my code:

    http.onreadystatechange = function() {
        console.log(http.readystate);
    }

I get "1, 2, 3, 4" - so the st

I'm following along to this video on YouTube: https://www.youtube.com/watch?v=h0ZUpPiV1ac&index=2&list=PL4cUxeGkcC9jAhrjtZ9U93UMIhnCc44MH

Can someone explain why I can't seem to get the different ready states?