Introduction to jQuery asynchronous object (XMLHttpRequest) _ javascript skills

Source: Internet
Author: User
The article divides jQuery asynchronous objects into five steps, which is very helpful for us to learn and remember. It is a very good article on learning jQuery asynchronous objects. We recommend it to you here. Let's take a look at the asynchronous object 5 callback.

This is a post request,

The Code is as follows:


// 1.00 create an asynchronous object
Var xhr = new XMLHttpRequest ();
// 2.0
Xhr. open ("post", url, params, true );
// 3.0 pass the parameter using the Formdata attribute
Xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
// 4.0 set the callback function
Xhr. onreadystatechange = function (){
If (xhr. readyState = 4 & xhr. status = 200 ){
Alert (xhr. responseText );
}
}
// 5.0 PASS Parameters
Xhr. send (params );

Encapsulate an asynchronous object with get requests

In the get request

Xhr. setRequestHeader ("If-Modified-Since", "0"); To clear the cache

And

The Code is as follows:


Xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");

It is for Transmission Mode
In
You can get three methods for the type in, including application/x-www-form-urlencoded

The Code is as follows:


Var ajaxHelp = {
CreatXHR: function (){
// Create an asynchronous object
Var xhr = new XMLHttpRequest ();
Return xhr;
},
// Ajax get request
AjaxGet: function (url, callBack ){
This. AJaxCommon ("get", url, null, callBack );
},
// Ajax post request
AjaxPost: function (url, params, callBack ){
This. AJaxCommon ("post", url, params, callBack );
},
AJaxCommon: function (method, url, params, callBack ){
// 1.0
Var xhr = this. CreatXHR ();
// 2.0
Xhr. open (method, url, true );
// 3.0
If (method = "get "){
Xhr. setRequestHeader ("If-Modified-Since", "0 ");
} Else {
Xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
}
// 4.0
Xhr. onreadystatechange = function (){
If (xhr. readyState = 4 & xhr. status = 200 ){
Var datas = JSON. parse (xhr. responseText );
// Execute the callback function
CallBack (datas );
}
}
// 5.0
Xhr. send (params );
}
};

Ps: In JQuery, there are asynchronous request methods such as $. ajax and $. get/$. Post. The previous package is no longer needed. Amount. Good luck. In fact, the underlying layer is also written like this. JQuery is designed to solve compatibility issues of various browsers.

The above is my understanding of jQuery asynchronous objects (XMLHttpRequest). If you have any omissions, please contact me and add me.

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.