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) AJAX Concepts A Simple AJAX Example

Steve Fan
Steve Fan
8,218 Points

Why it response when it hold the number 4

It mentioned that when it hold 4, it will response. And the syntax is: if (xhr.readyState === 4)

Could someone explain why the value is 4 but not anything else?

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

In short the readyState property changes from 0, 1, 2, 3, then 4 from the beginning (before request sent) to 4 at the end meaning that the request has been completed. A value of anything less than 4 means that the request hasn't been completed yet.

If you want to read more into what the different numbers mean check out these docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState

Steve Fan
Steve Fan
8,218 Points

Thank man, I got it.