This article describes the JS implementation of a simple and practical Ajax method. Share to everyone for your reference, specific as follows:
Copyright Wujxping//ajax 1.2//Update 2012-2-20//1, asynchronous data loading can be loaded get,post set//2, asynchronous synchronization Mode property settings//3, Data loading automatic timeout setting//4, * * * Add Data Loading events , through the event can carry on the server data real-time processing//5, increases the callback function the user custom parameter THIS.E//6, increases the Ajax repeatedly submits the control, only then defines the Ajax object as the global variable, each commit will wait for the last submitted execution result//7,
Modify the problem that XMLHTTP objects are repeatedly created when data is repeatedly submitted//8, fix major bugs, multiple Ajax event Overwrite issues//server data Return event Ajax.prototype.servereven=function (Func) { This.callback=new delegate (FUNC)//Instantiate}//Create asynchronous Processing Object Ajax.prototype.createxmlhttp=function () {if (This.xmlhttp!=null & amp;& typeof this. XmlHttp = = "Object") return this.
XmlHttp; Xmlhttpobj = ["Microsoft.XMLHTTP", "msxml2.xmlhttp.5.0", "msxml2.xmlhttp.4.0", "msxml2.xmlhttp.3.0", "MSXML2.XmlHttp
"]; Create XMLHttpRequest if (window) according to different browsers. ActiveXObject) {for (i=0;i<xmlhttpobj.length;i++) {//select IE compatible version try{this.
XmlHttp = new ActiveXObject (Xmlhttpobj[i]);
}catch (err) {continue; } if (this.
XmlHttp) break; ' Else if ' window. XMLHttpRequest) {this.
Xmlhttp=new XMLHttpRequest (); return this.
XmlHttp;
}Start calling Ajax.prototype.send=function () {if (this.isbusy)//ajax is busy return;
This.isbusy=true; var xmlhtml=this. Createxmlhttp (); Create object if (xmlhtml==null) {this.isbusy=false if (this.callback!=null) this.callback.run ("XMLHttpRequest Crea
Te faild! ", THIS.E);
Return
} var Url=this.url;
var _this=this;
Plus random number prevents caching if (Url.indexof ("?") > 0) url = + "&randnum=" + math.random ();
else URL = "? randnum=" + math.random ();
Xmlhtml.open (This.method,url,this.async);
Xmlhtml.setrequestheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8;");
Xmlhtml.setrequestheader ("Cache-control", "No-cache");
Xmlhtml.setrequestheader ("Connection", "keep-alive"); Open timed timeout waits for Var timer=settimeout (function () {//if (xmlhtml.readystate!=4) {xmlhtml.abort ();//cancel this transmission _this.i
Sbusy=false;
if (_this.callback!=null) _this.callback.run ("Send timeout!", _THIS.E); Cleartimeout (timer);
Close timer},this.timeout); if (This.async)//XORStep data load time state change with event Hook Xmlhtml.onreadystatechange=function () {///Receive server response if (xmlhtml.readystate==4) {//To determine whether the state is completed if (xmlhtml.status==200)
{//Judge whether to execute successful _this.isbusy=false; Cleartimeout (timer);
Turn off Timer if (_this.callback!=null)//Start triggering server event _this.callback.run (XMLHTML,_THIS.E);
}
}
};
try{xmlhtml.send (this.option);
}catch (Err) {This.isbusy=false cleartimeout (timer);//Turn off Timer alert (ERR);
Return
} if (!this.async) {//Synchronous data loading time data return processing this.isbusy=false; Cleartimeout (timer);
Close the timer if (this.callback!=null) This.callback.run (XMLHTML,THIS.E); }///Create Ajax object function Ajax (URL) {this.method= "post";//Set data submission mode this.async=true;//whether asynchronous data loading mode this.option= ""; The requested parameter this.url=url;//the requested URL connection this.timeout=1000*60*1;//The default timeout is 1 minutes this.e=null;//the user-defined parameter in the callback event this. xmlhttp=null;//receive asynchronously created objects prevent repeatedly creating this.isbusy=false//get current Ajax execution status this.callback=null;//declare callback event//Implementation Delegate class Delegate=fun CtiOn (func) {This.arr = new array ();//callback function Array This.add = function (func) {this.arr[this.arr.length] = func;
};
This.run = function (sender,e) {for (Var i=0;i<this.arr.length;i++) {var func = this.arr[i];
if (typeof func = = "function") {func (sender,e);//traverse all methods and Call} This.add (func);
}
}
For more information on AJAX-related content readers can view the site topics: "JavaScript Ajax Operating Skills summary" and "jquery Ajax Usage Summary"
I hope this article will help you with Ajax programming.