The Ajax of JavaScript

Source: Internet
Author: User

One: The actual meaning of the word ajax "does not occur page jumps, asynchronously load content and rewrite the content of the page technology", the advantage of asynchronous processing is not to let the user wait in vain

1.1 XMLHttpRequest: If you want to dynamically send a request to the server through JavaScript, you need to use the XMLHttpRequest object, the basic processing flow is as follows, after the XMLHttpRequest object is created, You need to specify the URL of the server to send the request

1 varXHR =NewXMLHttpRequest ();2Xhr.onreadystatechange =function(){3     if(Xhr.readstate ==4){4           if(Xhr.status = = 200){5 alert (xhr.responsetext);6           }7     }8 };9Xhr.open (' GET ', ' http://example.com/something ');TenXhr.setrequestheader (' if-modiified-since ', ' Thu ' June 1970 00:00;00 GMT '); OneXhr.send (NULL);

1.1.1 readystate Meaning: 0:open () has not been called, 1:send () has not been called, 2: The server has not returned Response 3: Accepting a response from the server 4: Completed response to the server received

The status code for the response is included in the 1.1.2 status, and if the communication ends normally, the value is 200. Additional Status codes for HTTP responses refer to other books

1.1.3 contains the string form of the server response in ResponseText. In the case of XML, the response is included in the Responsexml as a DOM object. Select the appropriate response type according to the situation. However, regardless of the form of the response, there will be a corresponding value in the ResponseText, so as long as the use of responsetext values there is no problem

1.1.4 passed to open () is the HTTP request type and the URL of the communication destination server, in fact, it can also accept the third parameter, if the third parameter is false, then Xmlhttpresponse will perform synchronous communication

1.1.5 setRequestHeader for request header settings

1.1.6 the actual send request to the server is send (), if it is a POST request type, the parameters will be sent to the server, if it is a get or head request, etc. do not need to send data request, the parameter is null

1.2 Timeout processing: In synchronous communication, the communication process is time-consuming, it will wait at send (), so it is best to set the appropriate timeout, if the time-out is canceled, the next step or request again

1 varXHR =NewXMLHttpRequest ();2 varTimerid =window.settimeout (funtion () {3 Xhr.abort ();4},500);//Five seconds will be timed out5Xhr.onreadystatechange =funtion () {6       if(request.readystate===4){7          //Cancel Timeout Processing8 window.cleartimeout (Timeid);9       }Ten};

1.3 Cross-domain communication:

JavaScript Ajax

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.