A simple and practical Ajax implementation

From , 3 Years ago, written in JavaScript, viewed 146 times.
URL https://pastebin.vip/view/034260c0
  1. // --------WUJXPING 2007-5-12
  2. //版权归属 WUJXPING
  3. //技术交流QQ396246973
  4. //ajax 1.2
  5. //更新2012-2-20
  6. //1、异步数据加载可以进行加载方式get,post的设定
  7. //2、异步同步模式的属性设定
  8. //3、数据加载自动超时设置
  9. //4、***数据加载事件的添加,通过事件可以进行服务器数据的实时处理
  10. //5、增加回调函数中用户自定义参数this.e
  11. //6、增加ajax反复提交控制,只需将ajax对象定义为全局变量,每次提交都会进行等待上次提交的执行结果
  12. //7、修改数据反复提交时XmlHttp对象被反复创建的问题
  13. //8、修复重大BUG,多个AJAX事件覆盖问题
  14.  
  15.  
  16. //服务器数据返回事件
  17. ajax.prototype.ServerEven=function(Func){
  18.         this.callback=new delegate(Func);//实例化
  19. }
  20.  
  21. //创建异步处理对象
  22. ajax.prototype.CreateXMLHttp=function(){
  23.         if(this.XmlHttp!=null && typeof this.XmlHttp == "object")
  24.                 return this.XmlHttp;
  25.         xmlhttpObj = ["Microsoft.XmlHttp","MSXML2.XmlHttp.5.0","MSXML2.XmlHttp.4.0","MSXML2.XmlHttp.3.0","MSXML2.XmlHttp"];
  26.    //根据不同的浏览器创建XMLHttpRequest
  27.     if(window.ActiveXObject){
  28.            for(i=0;i<xmlhttpObj.length;i++){ //选择ie兼容版本
  29.                         try{
  30.                                 this.XmlHttp = new ActiveXObject(xmlhttpObj[i]);
  31.                         }catch(err){
  32.                                 continue;
  33.                         }
  34.                         if(this.XmlHttp)
  35.                                 break;
  36.                 }
  37.     }
  38.     else if(window.XMLHttpRequest){
  39.        this.XmlHttp=new XMLHttpRequest();
  40.     }
  41.         return this.XmlHttp;
  42.  }
  43.  
  44.  //开始调用
  45. ajax.prototype.Send=function(){
  46.         if(this.isbusy)//ajax正忙
  47.                 return;
  48.         this.isbusy=true;
  49.     var xmlhtml=this.CreateXMLHttp(); //创建对象
  50.        
  51.         if(xmlhtml==null){
  52.                 this.isbusy=false
  53.                 if(this.callback!=null)
  54.                         this.callback.run("XMLHttpRequest Create Faild!",this.e);
  55.                 return;
  56.         }
  57.        
  58.         var url=this.url;
  59.         var _this=this;
  60.        
  61.         // 加随机数防止缓存
  62.         if (url.indexOf("?") > 0)
  63.                 url += "&randnum=" + Math.random();
  64.         else
  65.                 url += "?randnum=" + Math.random();
  66.        
  67.         xmlhtml.open(this.method,url,this.async);
  68.     xmlhtml.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8;");
  69.         xmlhtml.setRequestHeader("Cache-Control","no-cache");
  70.     xmlhtml.setRequestHeader("Connection","Keep-Alive");
  71.                
  72.         //开启定时进行超时等待
  73.         var timer=setTimeout(function(){
  74.                                                                 //if(xmlhtml.readyState!=4){
  75.                                                                 xmlhtml.abort(); //取消本次传输
  76.                                                                 _this.isbusy=false;
  77.                                                                 if(_this.callback!=null)
  78.                                                                         _this.callback.run("send timeout!",_this.e);
  79.                                                                 clearTimeout(timer); //关闭定时器
  80.                                                         },this.timeout);
  81.                
  82.        
  83.        
  84.         if(this.async)//异步数据加载时状态变化与事件挂钩
  85.                 xmlhtml.onreadystatechange=function(){//接收服务器响应
  86.                                                                                         if(xmlhtml.readyState==4){//判断是否是完成状态        
  87.                                                                                                 if(xmlhtml.status==200){ //判断是否执行成功                            
  88.                                                                                                         _this.isbusy=false;
  89.                                                                                                         clearTimeout(timer); //关闭定时器           
  90.                                                                                                         if(_this.callback!=null)//开始触发服务器事件
  91.                                                                                                                 _this.callback.run(xmlhtml,_this.e);                           
  92.                                                                                                 }
  93.                                                                                         }              
  94.                                                                                 };     
  95.                                                                                
  96.         try{
  97.                 xmlhtml.send(this.option);
  98.         }catch(err){
  99.                 this.isbusy=false
  100.                 clearTimeout(timer); //关闭定时器 
  101.                 alert(err);
  102.                 return;
  103.         }
  104.         if(!this.async){//同步数据加载时数据返回处理
  105.                 this.isbusy=false;
  106.                 clearTimeout(timer); //关闭定时器 
  107.                 if(this.callback!=null)
  108.                         this.callback.run(xmlhtml,this.e);
  109.         }
  110.  }
  111.  
  112.  //创建ajax对象
  113. function ajax(url){
  114.         this.method="post";//设置数据提交方式
  115.         this.async=true;//是否进行异步数据加载模式
  116.         this.option=""; //请求的参数
  117.         this.url=url;//请求的Url连接
  118.         this.timeout=1000*60*1;//默认超时时间为1分钟
  119.         this.e=null;//回调事件中用户自定义参数
  120.         this.XmlHttp=null;//接收异步创建的对象防止反复创建
  121.         this.isbusy=false//获取当前ajax的执行状态     
  122.         this.callback=null;//声明回调事件
  123.        
  124.         // 实现委托的类
  125.         delegate=function (func){
  126.             this.arr = new Array(); // 回调函数数组
  127.             this.add = function(func){
  128.                 this.arr[this.arr.length] = func;
  129.             };
  130.             this.run = function(sender,e){
  131.                 for(var i=0;i<this.arr.length;i++){
  132.                     var func = this.arr[i];
  133.                     if(typeof func == "function"){
  134.                         func(sender,e); // 遍历所有方法以及调用
  135.                     }
  136.                 }
  137.             }
  138.             this.add(func);
  139.         }
  140. }
  141.  
  142. //javascript/245

Reply to "A simple and practical Ajax implementation"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website