(1) onReadyStateChange Property
The onReadyStateChange property has functions that handle server responses. The following code defines an empty function that can be set at the same time as the onReadyStateChange property:
Xmlhttp.onreadystatechange=function ()
{
We need to write some code here.
}
(2) ReadyState property
The ReadyState property holds state information about the server response . The onreadystatechange function is executed whenever readyState changes.
This is the possible value of the ReadyState property:
Status description
0 The request was not initialized (before calling open ())
1 request has been made (before calling send ())
2 The request has been sent (here you can usually get the content header from the response)
3 in Request processing (usually some of the data in the response is available, but the server has not yet completed the response)
4 request completed (Can access server response and use it)
We want to add an if statement to this onreadystatechange function to test whether our response is complete (which means we can get the data):
Xmlhttp.onreadystatechange=function ()
{
if (xmlhttp.readystate==4)
{
Getting data from the server's response
}
}
(3) ResponseText property
The data returned by the server can be retrieved through the ResponseText property.
In our code, we will set the value of the time text box to equal to ResponseText:
Xmlhttp.onreadystatechange=function ()
{
if (xmlhttp.readystate==4)
{
Document.myform.time.value=xmlhttp.responsetext;
}
}
Other than that:
AJAX-Sending a request to the server
To send the request to the server, we need to use the open () method and the Send () method.
The open () method requires three parameters:
The first parameter defines the method (GET or POST) that is used to send the request.
GET is simpler and faster than POST, and can be used in most cases.
However, use the POST request in the following cases:
- Unable to use cache file (update file or database on server)
- Send a large amount of data to the server (POST has no data volume limit)
- Post is more stable and more reliable than GET when sending user input with unknown characters
The second parameter specifies the URL of the server-side script, which can be any type of file, such as. txt and. xml, or server script files, such as. asp and. PHP (able to perform tasks on the server before the response is returned)).
The third parameter specifies that the request should be processed asynchronously (true (asynchronous) or False (synchronous)).
The Send () method can send the request to the server. If we assume that the HTML file and the ASP file are in the same directory, then the code is:
Xmlhttp.open ("GET", "time.asp", true);
Xmlhttp.send (NULL);
From: http://hi.baidu.com/catprayer/blog/item/7e10a23c230c97e43c6d97f6.html (with Change)
Three properties of Ajax XMLHttpRequest objects and the open and send methods