六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 75|回复: 0

ajax封装类

[复制链接]

升级  4.67%

13

主题

13

主题

13

主题

秀才

Rank: 2

积分
57
 楼主| 发表于 2013-1-23 01:47:36 | 显示全部楼层 |阅读模式

  • function ajax(method,synchronous){   
  •     
  •     this._httpReq = false;  /*ajax初始化对象*/  
  •     this.method = method;  /*get|post*/  
  •     this.syn = synchronous;   /*是否采用异步请求,默认true*/  
  •     
  •     this.url = "";   /*提交异步请求的url地址*/  
  •     this.resType = "";  /*异步请求返回数据类型text|xml*/  
  •     this.callback = "";  /*异步请求完成后的回滚函数*/  
  •     this.loading = "";  /*load函数*/  
  •     this.content = null/*Ajax中send方法的参数*/  
  •     this.readystate = -1; /*ajax的请求状态*/  
  •     this.state = -1;  /*http请求响应代码*/  
  •     
  •      /************ get/set方法开始 ***************/  
  •      //设置提交异步请求的url地址   
  •      this.setUrl = function (url){   
  •           this.url = url;   
  •      }   
  •     
  •      //设置异步请求返回数据类型text|xml   
  •      this.setResType = function (restype){   
  •           this.resType = restype;   
  •      }   
  •     
  •      //设置回滚函数   
  •      this.setCallback = function (func){   
  •           this.callback = func;   
  •      }   
  •     
  •      //设置load函数   
  •      this.setLoading = function (loadFunc){   
  •           this.loading = loadFunc;   
  •      }   
  •     
  •      //设置send自带的参数值,默认null   
  •      this.setContent = function (contents){   
  •         this.content = contents;   
  •      }   
  •      /*********get/set方法结束*******/  
  •     
  •      /*********状态显示方法*********/  
  •      //调用window.alert方法   
  •      this.alert = function (msg){   
  •          window.alert(msg);   
  •      }   
  •     
  •      //调用window.status的方法   
  •      this.status = function (msg){   
  •           window.status = msg;   
  •      }   
  •      /*********状态显示方法结束*********/  
  •     
  •      /*************执行方法开始*****************/  
  •      //创建HttpXMLRequest   
  •      this.createXMLRequest = function(){   
  •      if(window.XMLHttpRequest){   
  •            this._httpReq = new XMLHttpRequest();   
  •       
  •            if(this._httpReq.overrideMimeType){   
  •                this._httpReq.overrideMimeType("text/xml");   
  •            }   
  •       }else if(window.ActiveXObject){   
  •             try{   
  •                  this._httpReq = new ActiveXObject("Msxml2.XMLHTTP");   
  •             }catch(e){   
  •                   try{   
  •                        this._httpReq = new ActiveXObject("Microsoft.XMLHTTP");   
  •                   }catch(e){}   
  •             }    
  •       }   
  •  }   
  •     
  •      //初始化ajax对象   
  •      this.init = function(){   
  •           this.createXMLRequest();   
  •      }   
  •     
  •      //发送一个http请求   
  •      this.send = function (){   
  •           if(this.resType.toLowerCase()=="post"){   
  •                   _httpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   
  •           }   
  •           this._httpReq.open(this.method,this.url,this.syn);   
  •           this._httpReq.send(this.content);   
  •      }   
  •     
  •         //取消一个http请求   
  •         this.abort = function (){   
  •                this._httpReq.abort();   
  •         }   
  •     
  •       this.callbackState = function(){   
  •             switch(this._httpReq.readyState){   
  •                    case 0:   
  •                             this.readystate = 0;   
  •                             break;   
  •                    case 1:   
  •                             this.readystate = 1;   
  •                             break;   
  •                    case 2:   
  •                             this.readystate = 2;   
  •                             break;   
  •                    case 3:   
  •                             this.readystate = 3;   
  •                             break;   
  •                    case 4:   
  •                             this.readystate = 4;   
  •                             switch(this._httpReq.status){   
  •                                    case 200:   
  •                                            eval(this.callback);   
  •                                            break;   
  •                                    case 202:   
  •                                            this.status("请求处理中,还没处理完毕!");   
  •                                            break;   
  •                                    case 400:   
  •                                            this.status("错误的请求!");   
  •                                            break;   
  •                                    case 404:   
  •                                            this.status("请求资源未找到!");   
  •                                            break;   
  •                                    case 500:   
  •                                            this.status("内部服务器错误,请联系管理员!");   
  •                                            break;   
  •                                    default:   
  •                                            this.status("返回数据失败,"+this._httpReq.status);   
  •                                            break;   
  •                               }   
  •                               break;   
  •                    default:   
  •                            this.readystate = 0;   
  •                            break;   
  •              }   
  •      }   
  •   
  •      this.onReadyStateChange = function (){   
  •             var owner = this;   
  •             this._httpReq.onreadystatechange = function(){   
  •                    owner.callbackState.call(owner);   
  •             }   
  •      }   
  •  /*************执行方法结束*****************/  

您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表