Mastering Ajax, Part 3:: Advanced Requests and Responses in Ajax
Mastering Ajax, Part 3:: Advanced Requests and Responses in Ajax
Level: Introductory
Brett McLaughlin (brett@newInstance.com), Author and Editor, O'Reilly Media Inc.
14 Feb 2006
For many Web developers, making simple
requests and receiving simple responses is all
they'll ever need, but for developers who
want to master Ajax, a complete
understanding of HTTP status codes, ready
states, and the XMLHttpRequest object is
required. In this article, Brett McLaughlin
will show you the different status codes and
demonstrate how browsers handle each and
he will showcase the lesser-used HTTP
requests that you can make with Ajax.
This is definitely the most common (and most simple) usage of ready states. As you might
guess from the number "4," though, there are several other ready states (you also saw this list in
the last article -- see Resources):
• 0: The request is uninitialized (before you've called open()).
• 1: The request is set up, but not sent (before you've called send()).
• 2: The request was sent and is in process (you can usually get content headers from the
response at this point).
• 3: The request is in process; often some partial data is available from the response, but
the server isn't finished with its response.
• 4: The response is complete; you can get the server's response and use it.
If you want to go beyond the basics of Ajax programming, you need to know not only these
states, but when they occur and how you can use them. First and foremost, you need to learn at
what state of a request you encounter each ready state. Unfortunately, this is fairly non-intuitive
and also involves a few special cases.
Ready states in hiding
The first ready state, signified by the readyState property 0 (readyState == 0), represents
an uninitialized request. As soon as you call open() on your request object, this property is set
to 1. Because you almost always call open() as soon as you initialize your request, it's rare to
see readyState == 0. Furthermore, the uninitialized ready state is pretty useless in practical
applications.
Still, in the interest of being complete, check out Listing 2 which shows how to get the ready
state when it's set to 0.
In this simple example, getSalesData() is the function that your Web page calls to start a
request (like when a button is clicked). Note that you've got to check the ready state before
open() is called. Figure 1 shows the result of running this application.
If you're not sure how to get this running, you'll need to create a function to call from your Web
page and have it send a request to a server-side component (just such a function was shown in
Listing 2 and throughout the examples in both the first and second articles in this series). Make
sure that when you set up your request, you set the callback function to updatePage(); to do
this, set the onreadystatechange property of your request object to updatePage().
This code is a great illustration of exactly what onreadystatechange means -- every time the
request's ready state changes, updatePage() is called and you see an alert. Figure 2 shows a
sample of this function being called, in this case with a ready state of 1.