We make sure to signal any other connection problems, though.
Handling the Server Response
When making an asynchronous request, such as in the code snippets presented earlier, the
execution of xmlHttp.send() doesn??™t freeze waiting for the server response; instead, the JavaScript
code continues executing. The process() function shown earlier defines a function named
handleRequestStateChange() as the callback method that should handle request state changes.
The readyState property can have one the following values representing the possible
states of the request:
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
Usually the handleRequestStateChange() method is called multiple times, when the request
enters a new stage, but you can??™t rely on the web browser to raise the readystatechange event for
all the mentioned states except state 4, which signals the request has completed.
The names of the states are self-explanatory except for state 3. The interactive state is an
intermediate state when the response has been partially received. In our AJAX applications we
will use only the complete state, which marks that a response has been fully received from the
server.
CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 407
The following code snippet shows the typical implementation of handleRequestStateChange
and highlights the portion where you actually get to read the response from the server:
// Function executed when the state of the request changes
function handleRequestStateChange()
{
// Continue if the process is completed
if (xmlHttp.
Pages:
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523