Ajax basic Knowledge sharing

Source: Internet
Author: User
Tags tidy

Tidy up a bit about Ajax knowledge, to share next!

Speaking of Ajax first of all we need to understand the principle of Ajax and XMLHttpRequest object: The principle of Ajax is simply the XMLHttpRequest object to the server to obtain data, and then use JavaScript to manipulate the DOM and update the page, One of the most critical steps is getting the request data from the server. Be clear about the process and the principle.

XMLHttpRequest is used to exchange data with the server in the background, which means that the part of the site can be updated without reloading the entire Web page, and when the request is sent to the server, the onReadyStateChange event is triggered whenever the readystate changes. , the readystate attribute holds XMLHttpRequest state information, and the following are three important properties of the XMLHttpRequest object:

1.onreadystatechange stores functions (or function names) whenever the ReadyState property is called.

2.readyState has a state of XMLHttpRequest, varying from 0 to 4:

1.0: The request was not initialized.

2.1: The server connection is established.

3.2: The request has been accepted.

4.3: in Request processing.

5.4: The request is complete and the response is ready.


1.200: "OK"

2.404: Page not found

4.status text string information accompanying the status code.

5.responseText returns a string form of data from the server process.

6.responseXML the DOM-compatible document data object returned from the server process.

Window.onload =function () {varOBTN = document.getElementById ('btn'); Obtn.onclick=function () {
To determine browser compatibility
var xhr;
if (window. XMLHttpRequest) {
XHR = new XMLHttpRequest ();
} else{
XHR = new ActiveXObject (' microsoft.xmlhttp ');
//create an object using XMLHttpRequest varXHR =NewXMLHttpRequest (); //Enter the transport mode, file address, synchronous, or asynchronous in the Address bar. Xhr.open ('Get','1.txt',true); //SubmitXhr.send (); //wait for the server to return contentXhr.onreadystatechange =function () {//readystate = = 4 indicates that the request is complete and the response is readyif(Xhr.readystate = =4) {alert (xhr.responsetext); } } } }

As shown above, the function first checks the overall state of the xmlhttprequest and guarantees that it completes the request.

Next I'm talking about how to send a request to the server:

1.OPEN Specifies the type of request, the URL, and whether the request is processed asynchronously, method: the type of request; Get or Post,url: The location of the file on the server. Async:true synchronous or asynchronous.

2.send (string) sends the request to the server. String: Only for post requests.

This is I tidy up some of the basics of Ajax, I hope to help you, but also please the Great GOD guidance!!!!!! 2016-08-03


Ajax basic Knowledge sharing

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.