Three attributes of the Ajax XMLHTTPRequest object and the open and send Methods

Source: Internet
Author: User

(1) onreadystatechange attributes
The onreadystatechange attribute contains functions for processing server responses. The followingCodeDefine an empty function and set the onreadystatechange attribute at the same time:

XMLHTTP. onreadystatechange = function ()
{
// We need to write some code here
}
(2) readystate attributes

The readystate attribute is stored on the server.Response status information. 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. The request has been submitted (before sending () is called)
2. The request has been sent (the content header can be obtained from the response)
3. The request is being processed (some data is usually available in the response, but the server has not yet 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
}
}
(3) 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;
}
}

In addition:

Ajax-send a request to the server
To send requests to the server, we need to useOpen () method and send ()Method.

The open () method requires three parameters:

The first parameter defines the method (get or post) used to send the request ).

Get is simpler and faster than post, and can be used in most cases.

However, use post requests in the following cases:

    • Unable to use cached files (updating files or databases on the server)
    • Send large amounts of data to the server (there is no limit on the size of post data)
    • When sending user input containing unknown characters, post is more stable and reliable than get.

The second parameter specifies the URL of the server script (This file can be any type of file, such as. txt and. XML, or a server script file, such as. asp and. php (the file can execute tasks on the server before returning a response)).

The third parameter specifies that requests should be processed asynchronously (True (asynchronous) or false (synchronous)).

The send () method can send requests to the server. If we assume that the HTML and ASP files are in the same directory, the Code is as follows:

XMLHTTP. Open ("get", "Time. asp", true );
XMLHTTP. Send (null );

From: http://hi.baidu.com/catprayer/blog/item/7e10a23c230c97e43c6d97f6.html (with modifications)

Related Article

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.