无为清净楼资源网 Design By www.qnjia.com
程序代码:
<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("您所请求的页面有异常。");
}
}
}
</script>



使用方法:
<script type="text/javascript">
var classAjax = new Ajax(sUrl,sRecvTyp,sQueryString,oResultFunc);
classAjax.post();//post方式发送数据
classAjax.get();//get方式发送数据
//sUrl发送地址
//sRecvTyp 接受数据类型:0为xml 1为text
//oResultFunc 返回结果处理函数
</script>

应用实例:
<script type="text/javascript">
var ajax1 = new Ajax("1.asp",0,"id="+id,bacal);
ajax1.post();
function bacal(rexm){
//返回数据处理函数;
}
</script>
标签:
一个Ajax类

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com