自己寫的ajax封裝js類

js 代碼
  1. function ajax(method,synchronous){   
  2.     
  3.     this._httpReq = false;  /*ajax初始化對象*/  
  4.     this.method = method;  /*get|post*/  
  5.     this.syn = synchronous;   /*是否採用異步請求,默認true*/  
  6.     
  7.     this.url = "";   /*提交異步請求的url地址*/  
  8.     this.resType = "";  /*異步請求返回數據類型text|xml*/  
  9.     this.callback = "";  /*異步請求完成後的回滾函數*/  
  10.     this.loading = "";  /*load函數*/  
  11.     this.content = null/*Ajax中send方法的參數*/  
  12.     this.readystate = -1; /*ajax的請求狀態*/  
  13.     this.state = -1;  /*http請求響應代碼*/  
  14.     
  15.      /************ get/set方法開始 ***************/  
  16.      //設置提交異步請求的url地址   
  17.      this.setUrl = function (url){   
  18.           this.url = url;   
  19.      }   
  20.     
  21.      //設置異步請求返回數據類型text|xml   
  22.      this.setResType = function (restype){   
  23.           this.resType = restype;   
  24.      }   
  25.     
  26.      //設置回滾函數   
  27.      this.setCallback = function (func){   
  28.           this.callback = func;   
  29.      }   
  30.     
  31.      //設置load函數   
  32.      this.setLoading = function (loadFunc){   
  33.           this.loading = loadFunc;   
  34.      }   
  35.     
  36.      //設置send自帶的參數值,默認null   
  37.      this.setContent = function (contents){   
  38.         this.content = contents;   
  39.      }   
  40.      /*********get/set方法結束*******/  
  41.     
  42.      /*********狀態顯示方法*********/  
  43.      //調用window.alert方法   
  44.      this.alert = function (msg){   
  45.          window.alert(msg);   
  46.      }   
  47.     
  48.      //調用window.status的方法   
  49.      this.status = function (msg){   
  50.           window.status = msg;   
  51.      }   
  52.      /*********狀態顯示方法結束*********/  
  53.     
  54.      /*************執行方法開始*****************/  
  55.      //創建HttpXMLRequest   
  56.      this.createXMLRequest = function(){   
  57.      if(window.XMLHttpRequest){   
  58.            this._httpReq = new XMLHttpRequest();   
  59.       
  60.            if(this._httpReq.overrideMimeType){   
  61.                this._httpReq.overrideMimeType("text/xml");   
  62.            }   
  63.       }else if(window.ActiveXObject){   
  64.             try{   
  65.                  this._httpReq = new ActiveXObject("Msxml2.XMLHTTP");   
  66.             }catch(e){   
  67.                   try{   
  68.                        this._httpReq = new ActiveXObject("Microsoft.XMLHTTP");   
  69.                   }catch(e){}   
  70.             }    
  71.       }   
  72.  }   
  73.     
  74.      //初始化ajax對象   
  75.      this.init = function(){   
  76.           this.createXMLRequest();   
  77.      }   
  78.     
  79.      //發送一個http請求   
  80.      this.send = function (){   
  81.           if(this.resType.toLowerCase()=="post"){   
  82.                   _httpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   
  83.           }   
  84.           this._httpReq.open(this.method,this.url,this.syn);   
  85.           this._httpReq.send(this.content);   
  86.      }   
  87.     
  88.         //取消一個http請求   
  89.         this.abort = function (){   
  90.                this._httpReq.abort();   
  91.         }   
  92.     
  93.       this.callbackState = function(){   
  94.             switch(this._httpReq.readyState){   
  95.                    case 0:   
  96.                             this.readystate = 0;   
  97.                             break;   
  98.                    case 1:   
  99.                             this.readystate = 1;   
  100.                             break;   
  101.                    case 2:   
  102.                             this.readystate = 2;   
  103.                             break;   
  104.                    case 3:   
  105.                             this.readystate = 3;   
  106.                             break;   
  107.                    case 4:   
  108.                             this.readystate = 4;   
  109.                             switch(this._httpReq.status){   
  110.                                    case 200:   
  111.                                            eval(this.callback);   
  112.                                            break;   
  113.                                    case 202:   
  114.                                            this.status("請求處理中,還沒處理完畢!");   
  115.                                            break;   
  116.                                    case 400:   
  117.                                            this.status("錯誤的請求!");   
  118.                                            break;   
  119.                                    case 404:   
  120.                                            this.status("請求資源未找到!");   
  121.                                            break;   
  122.                                    case 500:   
  123.                                            this.status("內部服務器錯誤,請聯繫管理員!");   
  124.                                            break;   
  125.                                    default:   
  126.                                            this.status("返回數據失敗,"+this._httpReq.status);   
  127.                                            break;   
  128.                               }   
  129.                               break;   
  130.                    default:   
  131.                            this.readystate = 0;   
  132.                            break;   
  133.              }   
  134.      }   
  135.   
  136.      this.onReadyStateChange = function (){   
  137.             var owner = this;   
  138.             this._httpReq.onreadystatechange = function(){   
  139.                    owner.callbackState.call(owner);   
  140.             }   
  141.      }   
  142.  /*************執行方法結束*****************/  
  143. }  


雖然跟網上有些ajax封裝類有些類似,但這個確實是我自己寫出來的,雖然寫得不是特別好,而且還有些功能還沒完善,不過這個js類的確可以用來在小項目使用一些ajax技術時非常方便。歡迎批評!!
發佈了5 篇原創文章 · 獲贊 0 · 訪問量 4753
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章