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


ajax readystate 1 2 3 4

i would understand better in detail what happen if i use 1,2,3 or 4 in the readystatechange function

if (xhr.readyState === 4) {}

1 Answer

readyState can actually be 0-4. Here is an explanation of what they mean. Each time the state changes, an onreadystatechange event occurs.

A new XMLHttpRequest object starts in state 0

When you successfully call .open() on the obect, the status changes to 1

When you successfully call .send() on the object, an HTTP request goes off to the server defined in .open(). Once the HTTP response headers have been received and processed the status changes to 2

Eventually, the actual content of the request will start coming in. When this begins, the status changes to 3

Finally, once all of the content is downloaded and is ready to be used, the status changes to 4