From the course: Ajax with PHP: Add Dynamic Content to Websites

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

States and events

States and events

From the course: Ajax with PHP: Add Dynamic Content to Websites

Start my 1-month free trial

States and events

- [Instructor] We learned how to send requests and we learned how to receive responses, but there's one big piece of the Ajax puzzle that's still missing. And that is how do we know when a response is ready? There's obviously going to be some elapsed time between when the request is first made and when the response is received. We can't assume that the response is ready right away. That's where states and events come in. The XML HTTP Request Object has another function called readyState and it will return a value which indicates the status of the current request. So you simply call xhr.readyState and you'll get back one out of five different values. And those values are going to be the numbers zero through four, zero, one, two, three, or four. And here's what each one means. If we get a zero it means the connection has been created, but not yet opened. One means the connection has been opened. Two means the request has been sent and received by the server. Three means that the…

Contents