自己写的简易ajax模型

/*
author zhangshuling
email  zhangshuling1214@126.com

*/
function  Ajax(){
    var _xmlHttp = null;
    this.createXMLHttpRequest = function(){
        try{
            if (window.ActiveXObject) {                                                       
                _xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");                                     
            }                                                                                 
            else if (window.XMLHttpRequest) {                                                     
                _xmlHttp = new XMLHttpRequest();                                               
            }
        }catch(e){
           alert(e.name +" : " + e.message);
        }
    }
   
    this.backFunction = function(_backFunction){
        if(_xmlHttp.readyState == 4) {
            if(_xmlHttp.status == 200) {
                _backFunction(_xmlHttp.responseXML);
            }
        }
         _xmlHttp.onreadystatechange = null;
    }

    this.doPost = function(_url,_parameter,_backFunction){
        try{
         _xmlHttp.open("POST",_url, false);
         _xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         _xmlHttp.send(_parameter);
         }catch(e){
          alert(e.name +" : " + e.message);
        }
    }
   
    this.doGet = function(_url,_parameter,_backFunction){
       try{
           var _random = Math.round(Math.random()*10000);
           _xmlHttp.open("GET", (_url+"?random=" +_random +"&" + _parameter), false);
           _xmlHttp.send(null);
         }catch(e){
            alert(e.name +" : " + e.message);
         }
    }
   
    this.ajaxRequest = function(_url,_parameter,_method,_backFunction){
          try{
            this.createXMLHttpRequest();
            if(_method.toLowerCase() == "post"){
               this.doPost(_url,_parameter,_backFunction);
            }else{
               this.doGet(_url,_parameter,_backFunction); 
            }
            try{
              _xmlHttp.onreadystatechange = this.backFunction(_backFunction);
            }catch(err){
               //??????IE?????????????????
            }
         }catch(e){
            alert(e.name +" : " + e.message);
         }
     }

}

/*
 var url = "ajax.do";
 var parameter = "parameter=parameter";
 var method = "post"
 
 function callBack(xml){
  ....
 }
 
 new Ajax().ajaxRequest(url,parameter,method,callBack);

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值