The principle of small white learning ajax-02-

Source: Internet
Author: User

Basic principle

The principle is simple.

    1. New XHR
    2. Xhr.onreadystatechange
    3. Xhr.open (method, URL, async)
    4. Xhr.send ()
Create a Xhr object

Compatible with the IE6, refer to Masaki greatly written:

var xhr = new (window. xmlhttprequest| | ActiveXObject) (' Microsoft.XMLHTTP ')
XHR Create request

Xhr.open (method, URL, async) is a create request and receives three parameters:

    1. The type of sending request, mainly "GET" and "POST";
    2. The requested URL, if the Get,data parameter is stitched behind the URL,
      How to Post,xhr.send (data), and set the header;
    3. Async is async, default is true to indicate async, and false for synchronization.

XHR Response Request

The Onreadychange object has a readystate attribute with a value of 5

0: not initialized. The open () method has not been called

1: Start. The open () method has been called, but the Send () method has not been called

2: Send. The Send () method has been called, but the response has not been received

3: Receive. Part of the response data has been accepted

4: Complete. All response data has been accepted and can be used on the client

Xhr.onreadystatechange = function() {  if (xhr.readystate = = = 4) {      var status = xhr.status;      if (Status >= & Status < | | status = = = 304) {          console.log (xhr.responsetext)  }}}  
Send Request

Xhr.send (data), the format of data is ' param1=value1&param2=value2 ';

It is important to note that when the type is post, the setRequestHeader is placed after Xhr.open ().

The principle of small white learning ajax-02-

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.