Java Reference
In-Depth Information
request.send(null);
 
var status = request.status;
 
if (status == 200) {
alert("The text file was found!");
} else {
alert("The server returned a status code of " + status);
}
This code performs the same basic function, but it only checks for a status code of 200 and sends a
generic message to alert the user for other status codes.
asynchronous requests
The previous code samples demonstrate the simplicity of synchronous requests. Asynchronous
requests, on the other hand, add some complexity to your code because you have to handle the
readystatechange event. In asynchronous requests, the XMLHttpRequest object exposes a
readyState property, which holds a numeric value; each value refers to a specific state in a request's
life span, as follows:
0 : The object has been created, but the open() method hasn't been called.
1 : The open() method has been called, but the request hasn't been sent.
2 : The request has been sent; headers and status are received and available.
3 : A response has been received from the server.
4 : The requested data has been fully received.
The readystatechange event fires every time the readyState property changes, calling the
onreadystatechange event handler. The fourth and final state is the most important; it lets you
know that the request completed.
Note   It is important to note that even if the request was successful, you
may not have the information you wanted. An error may have occurred on the
server's end of the request (a 404 , 500 , or some other error). Therefore, you still
need to check the status code of the request.
Code to handle the readystatechange event could look like this:
var request = new XMLHttpRequest();
 
function reqReadyStateChange() {
if (request.readyState == 4) {
var status = request.status;
 
if (status == 200) {
alert(request.responseText);
 
Search WWH ::




Custom Search