[Reprint] XMLHTTP synchronous and asynchronous data transmission

Source: Internet
Author: User
The client uses XMLHTTP to send a request to obtain the server response data, and uses JavaScript to operate the Dom to update the page. This is also called the brushless newest Update page) the submission method updates the web page trend.

XMLHTTP relies on XMLHttpRequest to complete the response from the client to the server. XMLHttpRequest provides two methods: open and send. The open method is used to initialize XMLHttpRequest.
Object, request method (get, post, etc.), secure connection, etc. after calling the open method, you must call the send method to send the HTTP request (HTTP request) to return HTTP reponse (HTTP response ).
Refer to the introduction to the send method in msdn:
This method is synchronous or asynchronous, depending on the value of the basync parameter in the open call. if open is called with basync = false, this call does not return until the entire response is already ed or the Protocol Stack times out. if open is called with basync = true, this call returns immediately.
Whether the send method is synchronous or asynchronous depends on the basync parameter in the open method. If basync = false, the send method works in the synchronous state. After an HTTP request is sent, only when the client receives all the response data from the server or the Protocol Stack times out! Otherwise, basync = true, which works asynchronously and returns directly.

In practice, set basync = true to make XMLHttpRequest work in asynchronous state after the send method is called. If it is set to synchronous state, it may lead to unnecessary long wait!

In addition, no matter how synchronous or asynchronous requests work, how does XMLHttpRequest obtain the response data returned by the server?
See the following sample code:

Program code <SCRIPT>
VaR XMLHTTP = NULL;
Function postorder (xmldoc)
{
Varxmlhttp = new activexobject ("msxml2.xmlhttp ");
XMLHTTP. Open ("Post", "http: // myserver/orders/processorder. asp", false );
XMLHTTP. onreadystatechange = handlestatechange;
XMLHTTP. Send (xmldoc );
}
Function handlestatechange ()
{
If (XMLHTTP. readystate = 4)
{
Alert ("result =" + XMLHTTP. responsexml. XML );
}
}
</SCRIPT>

The server returns the response data and is fully loaded. You can use the XMLHttpRequest attribute readstate to know that the value has changed to 4-completed (loaded ),
When the readstate changes, the onreadstatechange callback function in the XMLHTTPRequest object is called. In the function, the XMLHTTP. readystate = 4 is verified,
The XML document is obtained here (if the server returns XML document data ).

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.