An Ajax class

Source: Internet
Author: User

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>

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.