当前位置:文章中心 - 脚本应用 - 一个简单的ajax类
相关文章
这里放搜索
最新文章
一个简单的ajax类
关键词:简单,ajax类 时间:2008年07月28日 星期一 阅读:148

做网站中或者经常使用到ajax,尽管当前网上有许多ajax框架,其功能的确能强大,但其中日常使用的却不多,为了精简省略的目的,参考diszuz的ajax.js代码,总结出一个简单的ajax的类

程序代码
  1. /*    
  2. ajax类    
  3. */   
  4. function ajax(url,pars,fun)    
  5. {    
  6.         
  7.     var aj=new Object();        
  8.     aj.url=url;     
  9.     aj.pars=pars;       
  10.     aj.resultHandle=fun;        
  11.     aj.createXMLHttpRequest=function (){    
  12.             
  13.         var request=false;          
  14.         if(window.XMLHttpRequest){    
  15.                 
  16.             request=new XMLHttpRequest();               
  17.             if(request.overrideMimeType){    
  18.                     
  19.                 request.overrideMimeType('text/xml');                   
  20.                     
  21.             }    
  22.                 
  23.         }else if(window.ActiveXObject){    
  24.                 
  25.             var versions=['Microsoft.XMLHTTP','MSXML.XMLHTTP','Microsoft.XMLHTTP','Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP'];               
  26.             for(var i=0;i<versions.length;i++){    
  27.                     
  28.                 try{    
  29.                         
  30.                     request=new ActiveXObject(versions[i]);                     
  31.                     if(request){    
  32.                             
  33.                         return request;                         
  34.                             
  35.                     }    
  36.                         
  37.                 }catch(e){    
  38.                         
  39.                 }    
  40.                     
  41.             }    
  42.                 
  43.         }    
  44.         return request;         
  45.             
  46.     }    
  47.         
  48.     aj.XMLHttpRequest=aj.createXMLHttpRequest();        
  49.         
  50.     aj.processHandle=function (){    
  51.             
  52.         if(aj.XMLHttpRequest.readyState==4&&aj.XMLHttpRequest.status==200){    
  53.                 
  54.             aj.resultHandle(aj.XMLHttpRequest);             
  55.                 
  56.         }    
  57.             
  58.     }    
  59.         
  60.     aj.get=function (){    
  61.             
  62.         aj.XMLHttpRequest.onreadystatechange=aj.processHandle;          
  63.         aj.XMLHttpRequest.open("GET",aj.url+"?"+aj.pars);           
  64.         aj.XMLHttpRequest.send(null);           
  65.             
  66.     }    
  67.         
  68.     aj.post=function (){    
  69.             
  70.         aj.XMLHttpRequest.onreadystatechange=aj.processHandle;          
  71.         aj.XMLHttpRequest.open('POST',aj.url,true);         
  72.         aj.XMLHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');         
  73.         aj.XMLHttpRequest.send(aj.pars);            
  74.             
  75.     }    
  76.         
  77.     return aj;      
  78.         
  79. }  
上一篇:iframe跨浏览器高度自适应解决方法下一篇:类似QQ闪动效果

0条记录访客评论

暂未有任何评论,你来发表一篇吧!

发表评论

(必填)
(必填)
 
友情链接