懂得放弃,才会有新的收获!

Ajax的小方法(Version 2.1)

By weisnet On 2009-05-04 At 20:11 View:339

 
JavaScript
  1. ////////////////////Ajax////////////Class///////////////////
  2. //Power By Gloot CopyRight @2006
  3. //Edit Section for ... Version2.1
  4. //Blog http://blog.sina.com.cn/tecz
  5. //QQ 345268267
  6. ////////////////////////////////////////////////////////////
  7. var Try = {
  8.     these: function() {
  9.         var returnValue;
  10.         for (var i = 0; i < arguments.length; i++) {
  11.             var lambda = arguments[i];
  12.             try {
  13.                 returnValue = lambda();
  14.                 break;
  15.             } catch (e) {}
  16.         }
  17.     //alert(123);
  18.     return returnValue;
  19.     }
  20. }
  21. function grr(rp) {
  22.     if(RegExp.$1)/(.*)/.exec("");
  23.     var re="new RegExp("<result>(.*)<\/result>");
  24.     re.exec(rp);
  25.     if(RegExp.$1) return RegExp.$1;
  26.     return "";
  27. }
  28. function crr(rp) {
  29.     if(RegExp.$1)/(.*)/.exec("");
  30.     var re="new RegExp("<content>(.*)<\/content>");
  31.     re.exec(rp);
  32.     if(RegExp.$1) return RegExp.$1;
  33.     return "";
  34. }
  35. var Ajax = function() {}
  36. //var xhr ;这样定义不行
  37. Ajax.prototype.Init = function(){
  38.     return Try.these(
  39.         function() {return new ActiveXObject("MSXML2.XMLHTTP")},
  40.         function() {return new ActiveXObject("MSXML2.XMLHTTP.5.0")},
  41.         function() {return new ActiveXObject("MSXML2.XMLHTTP.4.0")},
  42.         function() {return new ActiveXObject("MSXML2.XMLHTTP.3.0")},
  43.         function() {return new ActiveXObject("Microsoft.XMLHTTP")},
  44.         function() {return new XMLHttpRequest()}
  45.     ) || false;
  46. }
  47. Ajax.prototype.Config = {
  48.     Result:"",
  49.     SucInfo:"",
  50.     FaildInfo:"",
  51.     Url:"",
  52.     returnType:"Compare",  //输入Compare是比较返回的字符是否一致,要指定Result值,//其他返回内容
  53.     ExecFunc:function(ty,w,reVal){ //w表示window,reVal:返回成功信息值
  54.         if (typeof ExecResult ="=" 'function')
  55.             ExecResult(ty,w,reVal);
  56.     },
  57.     sendData:""
  58. }
  59. Ajax.prototype.Action = function(url) {  //同步
  60.     url = url + '&e='+Math.random();
  61.     var xhr = this.Init();    
  62.     xhr.onreadystatechange = function()
  63.     {
  64.         if (xhr.readyState ="=" 4)
  65.         {
  66.             if (xhr.status ="=" 200)
  67.             {
  68.                 new Ajax().FuncResult(this.Config.Spn,xhr);    
  69.             }
  70.         }
  71.     }
  72.     xhr.open("POST",url,false);
  73.     xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  74.     xhr.send(null);
  75. }
  76. Ajax.prototype.Actionfor = function(url,spn) {  //异步
  77.     url = url + '&e='+Math.random();
  78.     var xhr = this.Init();  //这样写是为了多异步执行
  79.     xhr.onreadystatechange = function()
  80.     {
  81.         if (xhr.readyState ="=" 4)
  82.         {  
  83.             if (xhr.status ="=" 200)
  84.             {
  85.                 new Ajax().FuncResult(spn,xhr);    
  86.             }
  87.         }
  88.     }
  89.     xhr.open("GET",url,true);
  90.     xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  91.     xhr.send(null);  
  92. }
  93. Ajax.prototype.ActionAlert = function(url) //执行alert提示框的同步
  94. {
  95.     url = url + '&e='+Math.random();
  96.     var xhr = this.Init();  //这样写是为了多异步执行
  97.     xhr.onreadystatechange = function()
  98.     {
  99.         if (xhr.readyState ="=" 4)
  100.         {
  101.             if (xhr.status ="=" 200)
  102.             {
  103.                 new Ajax().AlertResult(xhr);    
  104.             }
  105.         }
  106.     }
  107.     xhr.open("GET",url,false);
  108.     xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  109.     xhr.send(null);
  110. }
  111. Ajax.prototype.ActionPost = function(url, spn) //同步 send
  112. {
  113.     url = url + '?e='+Math.random();
  114.     var xhr = this.Init();  //这样写是为了多异步执行
  115.     xhr.onreadystatechange = function()
  116.     {
  117.         if (xhr.readyState ="=" 4)
  118.         {
  119.             if (xhr.status ="=" 200)
  120.             {
  121.                 new Ajax().postResult(spn,xhr);    
  122.             }
  123.         }
  124.     }
  125.     
  126.     try {
  127.         if (netscape.security.PrivilegeManager.enablePrivilege)
  128.         {
  129.             netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
  130.             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  131.         }
  132.     }catch(e) {};
  133.     
  134.     xhr.open("POST",url,true);
  135.     
  136.     xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  137.     //xhr.setRequestHeader("Content-Length",pars.length);
  138.     xhr.setRequestHeader("Connection", "open");
  139.     
  140.     //alert(aj.Config.sendData)
  141.     xhr.send(this.Config.sendData);
  142. }
  143. Ajax.prototype.GetJSONData = function(url)
  144. {
  145.     url = url + '?e='+Math.random();
  146.     var xhr = this.Init();  //这样写是为了多异步执行
  147.     xhr.onreadystatechange = function()
  148.     {
  149.         if (xhr.readyState ="=" 4)
  150.         {
  151.             if (xhr.status ="=" 200)
  152.             {
  153.                 new Ajax().jsonResult(xhr);    
  154.             }
  155.         }
  156.     }
  157.     
  158.     xhr.open("GET", url, true);
  159.     
  160.     xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  161.     //xhr.setRequestHeader("Content-Length",pars.length);
  162.     xhr.setRequestHeader("Connection", "open");
  163.     
  164.     xhr.send(null);
  165. }
  166. /////////////////////////////////////////////////////////////////////////////////////////////////
  167. Ajax.prototype.onResult = function(v) {
  168.     return v="=this.Config.Result;
  169. }
  170. Ajax.prototype.FuncResult = function(spn,xhr)
  171. {  
  172.     
  173.     if (this.Config.returnType ="=" 'Compare')
  174.     {
  175.         if (this.onResult(grr(xhr.responseText)))
  176.         {
  177.             spn.innerHTML = this.Config.SucInfo;
  178.             this.Config.ExecFunc(this.Config.Result);
  179.             if (this.Config.Url!="'')
  180.             {
  181.                 window.location.href = this.Config.Url;
  182.             }
  183.         }
  184.         else
  185.         {
  186.             spn.innerHTML ="  this.Config.FaildInfo;
  187.         }
  188.     }
  189.     else
  190.     {
  191.         var cstr = crr(xhr.responseText);
  192.         
  193.         if (cstr != '') {
  194.             spn.innerHTML = crr(xhr.responseText);
  195.         }
  196.         this.Config.ExecFunc(this.Config.Result);
  197.         
  198.     }
  199. }
  200. Ajax.prototype.AlertResult = function(xhr)
  201. {
  202.     if (this.Config.returnType="='Compare')
  203.     {
  204.         if (this.onResult(grr(xhr.responseText)))
  205.         {
  206.             alert(this.Config.SucInfo);
  207.             this.Config.ExecFunc(this.Config.Result);
  208.         }
  209.         else
  210.         {
  211.             alert(this.Config.FaildInfo);
  212.         }
  213.     }
  214.     else
  215.     {
  216.         alert(crr(xhr.responseText));
  217.         this.Config.ExecFunc(this.Config.Result);
  218.     }
  219. }
  220. Ajax.prototype.postResult = function(spn,xhr)
  221. {
  222.     if (this.Config.returnType ="=" 'Compare')
  223.     {
  224.         if (this.onResult(grr(xhr.responseText)))
  225.         {
  226.             spn.innerHTML = this.Config.SucInfo;
  227.             this.Config.ExecFunc(this.Config.Result);
  228.         }
  229.         else
  230.         {
  231.             spn.innerHTML = this.Config.FaildInfo;
  232.         }
  233.     }
  234.     else
  235.     {
  236.         spn.innerHTML = crr(xhr.responseText);
  237.         var result = grr(xhr.responseText);
  238.         
  239.         this.Config.ExecFunc(this.Config.Result,window,result);
  240.     }
  241. }
  242. Ajax.prototype.jsonResult = function(xhr)
  243. {
  244.     var jsonstr = xhr.responseText;
  245.     if (jsonstr ="=" '')
  246.         jsonstr = '{}';
  247.     eval("json=" + jsonstr);
  248.     
  249.     //get data json.something json:{username:"123",content:""};
  250.     
  251.     this.Config.ExecFunc(this.Config.Result,window,json);
  252. }
  253. ///////////////////////////////////////////////////////////////
  254. var WebServices = function() {}
  255. WebServices.Config = {
  256. }

 
·上一篇:解决 Server Application Error 的方法
·下一篇:多级关联下拉(select)菜单代码

for this post

 
 

Leave a Reply

赶快抢个沙发坐坐!
请先 登陆  还没帐号

Hot Posts Powered by weiseditor

  • Add to Google

Friend links