Understanding Ajax step by step (2)

Source: Internet
Author: User

Ajax method: Load remote data through HTTP requests
Get method: load information through a remote http get request
POST method: load information through a remote http post request

1. Create an XMLHTTPRequest object

Function createxhr (){
Return window. XMLHttpRequest? New XMLHttpRequest (): New activexobject ("Microsoft. XMLHTTP ");
}

2. convert a key-value pair to a concatenation string
Function Params (data ){
VaR A = [];
For (var I in data ){
A. Push (encodeuricomponent (I) + "=" + encodeuricomponent (data [I]);
}
Return A. Join ("&");
}

3. encapsulate Ajax Methods
Parameters
Method Request methods get and post default get
Data key-Value Pair {key: Value}
URL
Cache true and false default true with Cache
Success success error exception
Function Ajax (ARGs ){
VaR xhr = createxhr ();
VaR DATA = Params (ARGs. data );
If (/get/I. Test (ARGs. Method) {// when the get method is used, the data is directly spliced to the URL.
Args. url + = "? "+ Data;
}
If (! Args. cache) {// No Cache
If (ARGs. url. indexof ("? ") <0) {// when no data parameter exists
Args. url + = "? ";
}
Args. url + = "&" + (new date (); // math. Random ();
}
Xhr. Open (ARGs. method, argS. url, true );
Xhr. onreadystatechange = function (){
If (4 = xhr. readystate & 200 = xhr. Status ){
Args. Success (xhr. responsetext, xhr. responsexml );
}
Else {
Args. Error ();
}
}
If (/post/I. Test (ARGs. Method )){
Xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
Xhr. Send (data );
}
Else {
Xhr. Send ();
}
}

4. This is a simple get Method Request function to replace complex Ajax methods. You can call the callback function when the request is successful. To execute a function when an error occurs, use the Ajax method.
Function get (URL, Data, FN ){
Ajax ({"method": "Get", "url": URL, "data": data, "success": fn });
}

5. This is a simple post Method Request function to replace the complex Ajax method. You can call the callback function when the request is successful. To execute a function when an error occurs, use the Ajax method.
Function post (URL, Data, FN ){
Ajax ({"method": "Post", "url": URL, "data": data, "success": fn });
}

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.