Also write a ajax.request class attached code _ajax related

Source: Internet
Author: User
Objective: Because some modules in the blog program need to use AJAX, the direct use of prototype.js volume is relatively large (more than 40 k), and only use the AJAX function, so in order to reduce the burden of downloading, It is not possible to change the code that has been written in the Prototype.js framework, only to write an Ajax class by itself in prototype style, reaching the 0 cost porting framework.

The new AJAX classes are as follows:

var Ajax = {Xmlhttp:function () {
try{
Return to new ActiveXObject (' msxml2.xmlhttp ');
}catch (e) {
try{
Return to 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];
}
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): "";
}
}
}
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 into a JS file, the volume is less than 1k, small enough. Ha ha.
Call Method:

var myajax = new Ajax.request (
"Http://localhost/abc.asp",
{
Method: "Post",
Parameters: "Demo=123456789abc",
Oncomplete:function (XMLHTTP) {
Alert (Xmlhttp.responsetext)
}
}
);

The style of the call is exactly the same as the original!

At present, this new class has only two callback functions: OnComplete and Onerror,ajax class also only request a method, after all, now blog program does not need so many applications. The Parameters property has a default value: {asynchronous:true,method: "Get", Parameters: ""}, which you can know if the call does not pass in asynchronous, method, Parameters three parameters, the class will use the default value.

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.