例
var ajax1 = new AJax("1.ASP",0,"id="+id,bacal);
AJax1.post();
function bacal(rexm){
返回數據處理函數;
}
程序代碼
function AJax(url,recvT,stringS,resultF) {
this.url = url;
this.stringS = stringS;
this.xmlHttp = this.createXMLHttpRequest();
if (this.XMLHttp == null) {
alert("erro");
return;
}
var objxml = this.XMLHttp;
objxml.onreadystatechange = function (){AJax.handleStateChange(objXML,recvT,resultF)};
}
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.stringS;
return queryString;
}
AJax.prototype.get = function () {
url = this.url;
var queryString = url+"?timeStamp=" + new Date().getTime() + "&" + this.createQueryString();
this.XMLHttp.open("GET",queryString,true);
this.XMLHttp.send(null);
}
AJax.prototype.post = function() {
url = this.url;
var url = url + "?timeStamp=" + new Date().getTime();
var queryString = this.createQueryString();
this.XMLHttp.open("POST",url,true);
this.XMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
this.XMLHttp.send(queryString);
}
AJax.handleStateChange = function (XMLHttp,recvT,resultF) {
if (XMLHttp.readyState == 4) {
if (XMLHttp.status == 200) {
resultF(recvT?xmlHttp.responseXML:XMLHttp.responseText);
} else {
alert("您所請求的頁面有異常。");
}
}
}