HTML and CSS Reference
In-Depth Information
Each time the readyState value changes, then the onreadystatechange handler is trig-
gered and the function assigned to it called.
When Is the Request Complete? When the server's response is complete, the
readyState value will be 4. The next step is to check this value, and if it is 4, retrieve the
data from the server's response.
To watch the server response change state, the following code can be added to your
program once the Ajax request object has been created. Assuming that the object is
named ajaxRequest, the following example tests the readyState values and sends an alert
stating what each value represents.
EXAMPLE (S AMPLE C ODE )
if(ajaxRequest.readyState == 0){
alert("There is no connection");
}
if(ajaxRequest.readyState == 1){
alert("The connection is loading");
}
if(ajaxRequest.readyState == 2){
alert("The data was loaded")
}
if(ajaxRequest.readyState == 3){
alert("Some data has been retrieved.
The connection is interactive");
}
if(ajaxRequest.readyState == 4){
alert("Complete! All of the data has been received");
}
Checking the HTTP Status. We might also want to check the HTTP status returned
by the server to the client to determine the outcome of a request. A status returned by
the HTTP server of 200 means that the response was successful. (In some of the follow-
ing examples using a PHP script, Firefox seems to return a status of 0.) An HTTP status
of 404 means that the URL cannot be found. There are a number of HTTP status values
that can be checked. Some common HTTP status codes are given in Table 18.4.
Table 18.4 Some HTTP Status Codes
HTTP Status Code
200 OK
OK. The server successfully returned the page.
400 Bad Request
Server didn't understand the request due to malformed syntax.
Continues
 
Search WWH ::




Custom Search