Ajax学习--笔记002

学了学JavaScript的类定义,封装了一个简单的Ajax类

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1 //Class XNAjax
  2 function XNAjax() {
  3     
  4 /*Private Member*/
  5     //XmlHttpRequest
  6     var xmlHttp;
  7     //URL
  8     var URL = "";
  9     //Post or Get
 10     var Method = "";
 11     //Synchronization or Asynchronous
 12     var IsSynchronization = true;
 13     //TimeOut
 14     var TimeOut = 0;
 15     //Parameters
 16     var Parameters = new Array();
 17     //Call Back Function
 18     var CallBackFun;
 19     //Is End
 20     var IsEndFlg = false;
 21     //Out Timer
 22     var OutTimer;
 23     //ExcuteResult
 24     var ExcuteResult = "";
 25 /*Private Member*/
 26 
 27 /*Property Start*/
 28 /*Property End*/
 29 
 30 /*Method Start*/
 31     //Init
 32     this.Init = function(url,method,issyn,timeout,callbackfun) {
 33         URL = url;
 34         Method = method;
 35         IsSynchronization = issyn;
 36         TimeOut = timeout;
 37         CallBackFun = callbackfun;
 38         IsEndFlg = true;
 39         createXmlHttpRequest();
 40     }
 41     
 42     //Add Send Parameter
 43     this.AddParameter = function (parm) {
 44         Parameters.push(parm);
 45     }
 46 
 47     //Add Send Parameter
 48     this.AddParameter1 = function (parmname,parmvalue) {
 49         Parameters.push(parmname + "=" + parmvalue);
 50     }
 51     
 52     //Create XmlHttpRequest
 53     var createXmlHttpRequest = function() {
 54         try {
 55             xmlHttp = new XMLHttpRequest();
 56         } catch(e) {
 57             var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
 58                                                 "MSXML2.XMLHTTP.5.0",
 59                                                 "MSXML2.XMLHTTP.4.0",
 60                                                 "MSXML2.XMLHTTP.3.0",
 61                                                 "MSXML2.XMLHTTP",
 62                                                 "Microsoft.XMLHTTP");
 63             for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)  {
 64                 try {
 65                     xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
 66                  } catch(e) {
 67                  
 68                  }
 69             }
 70         }
 71          
 72         if (!xmlHttp) {
 73             alert("Error:Can't Creat XMLHttpRequest."); 
 74         }
 75     }
 76     
 77     //Send
 78     this.Send = function() {
 79         if (Method == "GET") {
 80             if (Parameters.length != 0) {
 81                 URL = URL + "?" + Parameters.join("&"); 
 82             }
 83             xmlHttp.open('GET',URL,IsSynchronization); 
 84             xmlHttp.setrequestheader("content-type","application/x-www-form-urlencoded"); 
 85             xmlHttp.onreadystatechange = readyStateChange;
 86             xmlHttp.send(); 
 87         }
 88         
 89         if (Method == "POST") {
 90             xmlHttp.open('POST',URL,IsSynchronization); 
 91             xmlHttp.setrequestheader("content-type","application/x-www-form-urlencoded"); 
 92             xmlHttp.onreadystatechange = readyStateChange; 
 93             if (Parameters.length == 0) {
 94                 xmlHttp.send(); 
 95             } else {
 96                 xmlHttp.send(Parameters.join("&"));
 97                 
 98             }
 99         }
100         IsEndFlg = false;
101         if (TimeOut > 0) {
102             OutTimer = setInterval(timeOutFun,TimeOut * 1000);
103         }
104     }
105     
106     var readyStateChange = function() {
107         if (xmlHttp.readystate == 4) {
108             if (xmlHttp.status==200) {
109                 ExcuteResult = "SUCCESS";
110                 alert("OK");
111                 CallBackFun(xmlHttp.responseText);
112             } else {
113                 ExcuteResult = "ERROR";
114             }
115             IsEndFlg = true;
116             Parameters = new Array();
117         }
118     }
119     
120     this.Abort = function() {
121         xmlhttp.Abort();
122         IsEndFlg = true;
123         Parameters = new Array();
124     }
125     
126     var timeOutFun = function() {
127         if (!IsEndFlg) {
128             alert("Abort!");
129             xmlHttp.Abort();
130             ExcuteResult = "ERROR";
131             CallBackFun("");
132         }
133         Parameters = new Array();
134         clearInterval(OutTimer)
135     }
136     
137     this.IsSuccess = function() {
138         return (ExcuteResult == "SUCCESS");
139     }
140 /*Method End*/
141 }

 

转载于:https://www.cnblogs.com/jiangshui/archive/2009/03/25/1421808.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值