Simplified AJAX object

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.οnlοad=function(data){alert(data)};
}
Ajax.prototype={
        query:function(type,url,param,callback){
            isPOST=type.toLowerCase()=="post";
            if(typeof callback=="function")this.οnlοad=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.οnlοad=function(data){box.innerHTML=data;if(callback)callback(data)};
            this.get(url,param);
        }
}
new Ajax().get("ajaxpage.asp")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值