Ajax-XMLHTTPRequest object

Source: Internet
Author: User
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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.