Program code:
<Script type = "text/javascript">
Function Ajax (sUrl, sRecvTyp, sQueryString, oResultFunc ){
This. Url = sUrl;
This. QueryString = sQueryString;
This. XmlHttp = this. createXMLHttpRequest ();
If (this. XmlHttp = null ){
Alert ("erro ");
Return;
}
Var objxml = this. XmlHttp;
Objxml. onreadystatechange = function () {Ajax. handleStateChange (objxml, sRecvTyp, oResultFunc )};
}
Ajax. prototype. createXMLHttpRequest = function (){
Try {return new ActiveXObject ("Msxml2.XMLHTTP");} catch (e ){}
Try {return new ActiveXObject ("Microsoft. XMLHTTP");} catch (e ){}
Try {return new XMLHttpRequest ();} catch (e ){}
Return null;
}
Ajax. prototype. createQueryString = function (){
Var queryString = this. QueryString;
Return queryString;
}
Ajax. prototype. get = function (){
SUrl = this. Url;
Var queryString = sUrl + "? TimeStamp = "+ new Date (). getTime () +" & "+ this. createQueryString ();
This. XmlHttp. open ("GET", queryString, true );
This. XmlHttp. send (null );
}
Ajax. prototype. post = function (){
SUrl = this. Url;
Var sUrl = sUrl + "? TimeStamp = "+ new Date (). getTime ();
Var queryString = this. createQueryString ();
This. XmlHttp. open ("POST", sUrl, true );
This. XmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
This. XmlHttp. send (queryString );
}
Ajax. handleStateChange = function (XmlHttp, sRecvTyp, oResultFunc ){
If (XmlHttp. readyState = 4 ){
If (XmlHttp. status = 200 ){
OResultFunc (sRecvTyp? XmlHttp. responseXML: XmlHttp. responseText );
} Else {
Alert ("the page you requested has an exception. ");
}
}
}
</Script>
Usage:
<Script type = "text/javascript">
Var classAjax = new Ajax (sUrl, sRecvTyp, sQueryString, oResultFunc );
ClassAjax. post (); // send data in post Mode
ClassAjax. get (); // send data in get Mode
// SUrl sending Address
// SRecvTyp accept data type: 0 is xml 1 is text
// OResultFunc return result processing function
</Script>
Application instance:
<Script type = "text/javascript">
Var ajax1 = new Ajax ("1.asp", 0," id = "+ id, bacal );
Ajax1.post ();
Function bacal (rexm ){
// Return the data processing function;
}
</Script>