基本用法:
複製程式碼 程式碼如下:
var ajax = new AjaxObj(url);
istener(200, function(r){
alert(r);
});
();
也可以連續呼叫:
複製程式碼 程式碼如下:
var ajax = new AjaxObj(url)istener(200, function(r){
alert(r);
})();
另外還支援自定義的`POST或GET方式請求,以及監視不同的HTTP狀態碼,自己看程式碼琢磨吧 :)
完整程式碼:
複製程式碼 程式碼如下:
AjaxObj = function(url, method, content){
this.r = null;
= url;
od = method;
ent = content;
er = {};
er["Connection"] = "close";
er["Content-type"] = "application/x-www-form-urlencoded";
var self = this;
if(ttpRequest){
this.r = new XMLHttpRequest();
}else if(veXObject){
try {
this.r = new ActiveXObject("TTP");
} catch(e) {
try{
this.r = new ActiveXObject("TTP");
} catch(e) {
}
}
}
istener = function(http_status, func){
if(!this.L)
this.L=[];
this.L[http_status] = func;
return this;
};
eader = function(name, value){
er[name] = value;
equestHeader(name, value);
return this;
};
= function(){
if(od != "post" && od != "get")
od = "get";
(od, , true);
for(var h in er) {
equestHeader(h, er[h]);
}
(ent);
};
if(this.r) adystatechange = function(){
if(yState == 4 && self.L[us] != null)
self.L[us](onseText);
};
};