学了学JavaScript的类定义,封装了一个简单的Ajax类
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 }
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 }