一个简单的Ajax类

JavaScript代码
  1. /**  
  2.     Powered By CMSDream Copyright © 2007-2008 All rights reserved.  
  3.     14:21 2008-5-10  
  4. **/  
  5. function CMSDreamAjaxLib(){   
  6.     /**//**  
  7.     成员变量  
  8.     */  
  9.     this.XMLHttpReq = null;         //XML对象   
  10.     this.method = "post";           //执行的方法(post/get)   
  11.     this.url = "";              //异步调用的页面地址   
  12.     this.responseText = "";         //异步返回的响应字符串   
  13.     this.responseXML = "";          //异步返回的响应XML   
  14.     this.failed = false;            //创建对象错误标志   
  15.        
  16.     /**//**  
  17.     事件区  
  18.     */  
  19.     this.onLoading = function() {};     //正在发送请求   
  20.     this.onLoaded = function() {};      //已经接收到全部响应内容   
  21.     this.onInteractive = function() {}; //正在解析响应内容   
  22.     this.onCompletion = function() {};  //响应内容解析完成   
  23.     this.onError = function() {};       //异步错误处理事件   
  24.     this.onFail = function() {};        //创建对象失败处理世界   
  25.        
  26.     /**//**  
  27.     重置所有事件函数  
  28.     */  
  29.     this.resetFunctions = function() {   
  30.         this.onLoading = function() {};   
  31.         this.onLoaded = function() {};   
  32.         this.onInteractive = function() {};   
  33.         this.onCompletion = function() {};   
  34.         this.onError = function() {};   
  35.         this.onFail = function() {};   
  36.     };   
  37.     
  38.     /**//**  
  39.     初始化函数(构造时自动初始化)  
  40.     */  
  41.     this.init = function(){   
  42.         if(window.XMLHttpRequest){   
  43.             this.XMLHttpReq = new XMLHttpRequest();   
  44.         }else if (window.ActiveXObject){   
  45.             try{this.XMLHttpReq = new ActiveXObject("Msxml4.XMLHTTP");}   
  46.             catch(e){   
  47.                 try{this.XMLHttpReq = new ActiveXObject("Msxml3.XMLHTTP");}   
  48.                 catch(e){   
  49.                     try{this.XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");}   
  50.                     catch(e){   
  51.                         try{this.XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");}   
  52.                         catch(oc){this.failed=true;}   
  53.                     }   
  54.                 }   
  55.             }   
  56.         }   
  57.     };   
  58.        
  59.     /**//**  
  60.     发送请求函数  
  61.     @param data 发送的数据  
  62.     @example send("id=1");  
  63.     */  
  64.     this.send=function(data){   
  65.         var self=this;   
  66.         if(this.method=="post") {   
  67.             this.XMLHttpReq.open(self.method,self.url,true);   
  68.         }else{   
  69.             this.XMLHttpReq.open(self.method,self.url+"?"+encodeURI(data),true);   
  70.         }   
  71.         //添加消息响应头   
  72.         this.XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");   
  73.            
  74.         //异步回调函数   
  75.         this.XMLHttpReq.onreadystatechange = function(){   
  76.             //对象未创建   
  77.             if (self.failed) {   
  78.                 self.onFail();   
  79.                 return;   
  80.             }   
  81.                
  82.             //消息响应标志   
  83.             switch (self.XMLHttpReq.readyState) {   
  84.                 case 1:{   
  85.                     self.onLoading();   
  86.                     break;   
  87.                 }   
  88.                 case 2:{   
  89.                     self.onLoaded();   
  90.                     break;   
  91.                 }   
  92.                 case 3:{   
  93.                     self.onInteractive();   
  94.                     break;   
  95.                 }   
  96.                 case 4:{   
  97.                     self.responseText = self.XMLHttpReq.responseText;   
  98.                     self.responseXML = self.XMLHttpReq.responseXML;   
  99.                     self.onCompletion();   
  100.                     break;   
  101.                 }   
  102.             }   
  103.         };   
  104.            
  105.            
  106.         if(this.method=="post"){   
  107.             this.XMLHttpReq.send(data); //发送请求   
  108.         }else{   
  109.             this.XMLHttpReq.send();     //发送请求   
  110.         }   
  111.     };   
  112.        
  113.     this.abort=function(){   
  114.         this.XMLHttpReq.abort();   
  115.     }   
  116.        
  117.     this.close=function(){   
  118.         this.XMLHttpReq=null;   
  119.     }   
  120.     this.init();   
  121. }  
引用通告地址: 点击获取引用地址
标签: ajax javascript
评论: 0 | 引用: 0 | 阅读: 617 | 打印 | 打包
发表评论
昵 称: 密 码:
网 址: 邮 箱:
验证码: 验证码图片 选 项:
头 像:
内 容: