Also write an Ajax. Request class with code

Source: Internet
Author: User

Purpose: Because blog Program Some modules in need to use Ajax and directly use prototype. JS is relatively large (more than 40 K), and only uses the Ajax function. Therefore, to reduce the download burden, you cannot change the prototype already in use. written under the JS framework Code You can only write an Ajax class according to the prototype style to achieve zero-cost porting framework.

The new Ajax class is as follows:

VaR Ajax = {XMLHTTP: function (){
Try {
Return new activexobject ('msxml2. xmlhttp ');
} Catch (e ){
Try {
Return new activexobject ('Microsoft. xmlhttp ');
} Catch (e ){
Return new XMLHttpRequest ();
}
}
}
};

Ajax. request = function () {
If (arguments. length <2) return;
VaR _ p = {asynchronous: True, method: "Get", parameters :""}; // default option
for (var key in arguments [1]) {// custom option overwrite default option
_ p [Key] = arguments [1] [Key];
}< br> VaR _ x = Ajax. XMLHTTP (); // xml obj
VaR _ url = arguments [0]; // STR
If (_ p ["Parameters"]. length> 0) _ p ["Parameters"] + = '& _ =';
If (_ P ["method"]. touppercase () = "get") _ URL + = (_ URL. Match (/\? /)? '&':'? ') + _ P ["Parameters"];
_ x. open (_ p ["method"], _ URL, _ p ["Asynchronous"]);
_ x. onreadystatechange = function () {
If (_ x. readystate = 4) {
If (_ x. status = 200) {
_ p ["oncomplete"]? _ P ["oncomplete"] (_ x): "";
}else {
_ p ["onerror"]? _ P ["onerror"] (_ x): "";
}< BR >}< br> If (_ p ["method"]. touppercase () = "Post") _ x. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded");
_ x. send (_ p ["method"]. touppercase () = "Post "? _ P ["Parameters"]: NULL);
};

This class is saved as a JS file, which is smaller than 1 kb. Haha.
Call method:

VaR myajax = new Ajax. Request (
"Http: // localhost/ABC. asp ",
{
Method: "Post ",
Parameters: "demo = 123456789abc ",
Oncomplete: function (XMLHTTP ){
Alert (XMLHTTP. responsetext)
}
}
);

The Calling style is exactly the same as the original one!

Currently, this new class only has two callback functions: oncomplete and onerror. The Ajax class only has one request method. After all, there is no need for so many applications in the blog program. The parameters property has a default value: {asynchronous: True, method: "Get", parameters: ""}, which can be known from this, if the asynchronous, method, and parameters are not input during the call, the default value is used for the class.

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.