自己写的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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章