An Ajax class _ajax related

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 are requesting has an exception.) ");
}
}
}
</script>



How to use:
<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 Send Address
Srecvtyp Accept Data type: 0 is XML 1 is text
Oresultfunc return result handler function
</script>

application Example:
<script type= "Text/javascript" >
var ajax1 = new Ajax ("1.asp", 0, "id=" +id,bacal);
Ajax1.post ();
function Bacal (REXM) {
Returns the data processing function;
}
</script>
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.