发表时间:2008-12-12
最后修改:2008-12-12
function Ajax(){
this.XMLHttp=false;
try {this.XMLHttp=new XMLHttpRequest()}catch (e1){
try {this.XMLHttp=new ActiveXObject("Msxml2.XMLHTTP")} catch (e2){
try {this.XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")} catch (e3){this.XMLHttp = false}}};
this.time=2000;
this.encode="GB2312";
this.onload=function(data){alert(data)};
}
Ajax.prototype={
query:function(type,url,param,callback){
isPOST=type.toLowerCase()=="post";
if(typeof callback=="function")this.onload=callback;
var exURL=this.setURL(url,param,isPOST);
this.XMLHttp.open(type,exURL[0]);
this.XMLHttp.setRequestHeader("cache-control","no-cache");
this.XMLHttp.setRequestHeader("Charset",this.encode);
if(isPOST){this.XMLHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded")};
var _this=this;
this.XMLHttp.onreadystatechange=function(){
if(_this.XMLHttp.readyState!=4)return;
var data=null;
if(_this.XMLHttp.status==200){data=_this.XMLHttp.responseText};
_this.onload(data);
};
POSTVal=isPOST?exURL[1]:null;
this.XMLHttp.send(POSTVal);
setTimeout(function(){_this.XMLHttp.abort()},this.time);
},
setURL:function(url,params,isPOST){
var param=[];
for(var key in params){param.push(escape(key)+"="+escape(params[key]))};
if(isPOST)return [url,param.join("&")];
param.push("cache="+Date.parse(new Date()));
return [url+"?"+param.join("&")];
},
post:function(url,param,callback){this.query("POST",url,param,callback)},
get:function(url,param,callback){this.query("GET",url,param,callback)},
load:function(box,url,param,callback){
var box=typeof(box)=="object"?box:document.getElementById(box);
this.onload=function(data){box.innerHTML=data;if(callback)callback(data)};
this.get(url,param);
}
}
new Ajax().get("ajaxpage.asp")