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方式發(fā)送數(shù)據(jù) classAjax.get();//get方式發(fā)送數(shù)據(jù) //sUrl發(fā)送地址 //sRecvTyp 接受數(shù)據(jù)類型:0為xml 1為text //oResultFunc 返回結(jié)果處理函數(shù) /script>
應用實例: script type="text/javascript"> var ajax1 = new Ajax("1.asp",0,"id="+id,bacal); ajax1.post(); function bacal(rexm){ //返回數(shù)據(jù)處理函數(shù); } /script>