Document directory
- Onreadystatechange attribute
- Readystate attributes
- Responsetext attributes
Ajax-learn more about XMLHttpRequest objects
Before sending data to the server, it is necessary to explain the three important attributes of the XMLHTTPRequest object.
Onreadystatechange attribute
The onreadystatechange attribute contains functions for processing server responses. The following code defines an empty function. You can set the onreadystatechange attribute at the same time:
xmlHttp.onreadystatechange=function()
{
// We need to write some code here
}
Readystate attributes
The readystate attribute contains the status information of the server response. When the readystate changes, the onreadystatechange function is executed.
This is the possible value of the readystate attribute:
| Status |
Description |
| 0 |
Request not initialized (before calling open) |
| 1 |
Request Submitted (before sending () is called) |
| 2 |
The request has been sent (here we can usually get the Content Header from the response) |
| 3 |
Request Processing (some data is usually available in the response, but the server has not completed the response) |
| 4 |
The request has been completed (you can access the server response and use it) |
We need to add an if statement to this onreadystatechange function to test whether our response has been completed (meaning data can be obtained ):
XMLHTTP. onreadystatechange = function ()
{
If (xmlHttp.readyState==4)
{
// Obtain data from the server's response
}
}
Responsetext attributes
You can use the responsetext attribute to retrieve the data returned by the server.
In our code, we will set the value of the time text box to be equal to responsetext:
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}