The status of the XHR request makes a number of calls to the onreadystatechange event
handler. From within the event handler, you can check the status of the call by checking the
readyState property on the XHR object: transport. The readyState property will store a number
between 0 and 4 at any given time (see Table 5-1).
Table 5-1. Possible readyState States
Value State Description
0 Uninitialized The open method of the XHR object hasn??™t been called yet.
1 Loading The send method hasn??™t been called yet.
2 Loaded The send method has been called; response headers are available.
3 Interactive The response data is in the process of downloading, and is available via
the responseText property of the XHR object.
4 Completed Everything is done, and the entire result is available in the responseText
and, if available, the responseXML properties of the XHR object.
CHAPTER 5 n AJAX AND DATA EXCHANGE 101
It is standard practice to check for a readyState of 4 (it is the XHR equivalent of
window.onload):
transport.onreadystatechange = function(){
if(transport.readyState == 4)
{
alert('I am done!');
}
};
That??™s all there is to it. Okay, not really. There is much more to consider, starting with the
request/response process.
Ajax Request/Response Process
In a traditional request, a user initiates a request for data, waits for the server to send back a
response, and then waits for the browser to render the page.
Pages:
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167