Three properties of Ajax XMLHttpRequest objects and the open and send methods

Source: Internet
Author: User

(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)

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: ("GET", "time.asp", true);
Xmlhttp.send (NULL);

From: (with Change)

Three properties of Ajax XMLHttpRequest objects and the open and send methods

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: 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.