The process of implementing asynchronous requests by Ajax

Source: Internet
Author: User

var xhr;
XHR = new XMLHttpRequest (); Create an Asynchronous object
Xhr.open ("Get", "Test.ashx", true); The three parameters in the get way brackets are: 1. How to send a request 2. Page 3 of the sample request. Whether asynchronous
Xhr.open ("Post", "Test.ashx", true);
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Post mode to send data


This callback function is primarily used to detect whether the server is returning data to an asynchronous object
Xhr.setrequestheader ("If-modified-since", "0"); Set the browser not to use the cache
Xhr.onreadystatechange = function () {
if (xhr.readystate = = 4) {
The ReadyState property indicates several states in which the XMLHttpRequest object is in the process of sending/receiving data. The XMLHttpRequest object undergoes 5 different states.
0: not initialized. Object has been created, but not initialized yet, the open method has not been called;
1: Open. The object has been created and initialized, but the Send method has not been called;
2: Sent. The Send method has been called, but the object is waiting for the status code and header to return;
3: Receiving. Some of the data has been received, but the properties and methods of the object cannot be used, because the state and response headers are incomplete;
4: Loaded. All data received


if (xhr.status==200) {//The status code of the response message returned by the detection server is 200
alert (Xhr.responsetext); Response data returned by the server
Parsing the data returned by the server in Jason format
var S=xhr.responsetext;
var json=eval ("(" +s+ ")");
alert (jason.data);
}

};
};
Xhr.send (NULL); Asynchronous object Send Request
Xhr.send ("txtname=by&txtpwd=0703"); Send data as Post

The process of implementing asynchronous requests by Ajax

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.