在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
程序代码: <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> |
请发表评论