The Ready State

After the XHR object has been created and the request has been made, we need a way to know when the response has been received. This is where the onreadystatechange event handler is used. The onreadystatechange event handler fires when the state of the request object changes and allows us to set a callback method to be triggered. After this callback method is triggered, it is up to us to handle the response. The custom callback method named onResponse, shown in Listing 2.1, will be covered in Chapter 3, where we will cover all aspects of an Ajax response.

Listing 2.1. Sending a Request
function sendRequest(url)
{
    request.onreadystatechange = onResponse;
    request.open("GET", url, true);
    request.send(null);
}

Listing 2.2 is a custom ...

Get Ajax for Web Application Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.