平时使用多了,就直接封装起来,比较方便,呵呵

当然,大家要自己用的,也可以修改一下,适合自己用

 

*
 *     封装Ajax 传输类
 *     URL:传过去处理的页面地址
 *    callBackFunction:成功后调用的方法
 *    errorFunction:失败后调用的方法
 *    用法: var mAjaxer = new Ajaxer("url",callBackFunction,errorFunction)
 *                 mAjaxer.send();
 *  by:huwei
 */
var Ajaxer = function(URL,callBackFunction,errorFunction){
    
this.AjaxMethod = "POST";                            //默认传输方式 POST;
    this.SendObject = null;                                //传输的内容,默认null    
    this.ResponseType = "Text";                        //返回值类型 
    this.Async = true;                                        //是否异步方式
    
    
function createXMLHttp(){
    
var xmlhttp;
    
try{
        xmlhttp 
= new ActiveXObject("Msxml2.XMLHTTP");
    }
catch(ex){
        
try{
            xmlhttp 
= new ActiveXObject("Microsoft.XMLHTTP");
         }
catch(ex){
            xmlhttp 
= new XMLHttpRequest();
          }
        }
    
return xmlhttp;
    }

    
var xmlHttp;
    
     
this.send =    function ()
    {
        xmlHttp 
= null;
        xmlHttp 
= createXMLHttp();
        
if(xmlHttp == null)
        {
            alert(
"创建xmlHTTP失败!");
        }
else
        {
            xmlHttp.onreadystatechange 
= this.SendBack;
            xmlHttp.open(
this.AjaxMethod,URL,this.Async);
            xmlHttp.setRequestHeader(
"Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(
this.SendObject);
        }
    }
    
    
this.SendBack = function (){
        
if(xmlHttp.readyState == 4){
            
if(xmlHttp.status == 200){
                
var res;
                res 
= xmlHttp.responseText;
                callBackFunction(res);                
            }
else{
                
var error = eval(error + "=" + "{\"code\":\"" + xmlHttp.status + "\",\"message\":\"" + xmlHttp.statusText + "\"}" );
                errorFunction(error);
            }
        }
    }
}

 

/Files/bboy/Ajaxer.rar

相关文章: