Basic principle
The principle is simple.
- New XHR
- Xhr.onreadystatechange
- Xhr.open (method, URL, async)
- 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:
- The type of sending request, mainly "GET" and "POST";
- The requested URL, if the Get,data parameter is stitched behind the URL,
How to Post,xhr.send (data), and set the header;
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¶m2=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-