程式碼:
<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>