JavaScript must be enabled in order for you to view this page. However, it seems JavaScript is either disabled or not supported by your browser. To view this page, enable JavaScript by changing your browser options, then Try again! .

封装XHR隐藏浏览器相关性

by solo L2008-04-03T22:00:00Z,tag:Ajax

 

 

  1 /**
  2  * For transport.
  3  *
  4  * @author Solo L(www.solol.org)
  5  */
  6 
  7 /**
  8  * From Prototype Library.
  9  * 
 10  * @author Solo L(www.solol.org)
 11  */
 12 var Ajax = {
 13   getTransport: function() {
 14     return Try.these(
 15       function() {return new XMLHttpRequest()},
 16       function() {return new ActiveXObject('Msxml2.XMLHTTP')},
 17       function() {return new ActiveXObject('Microsoft.XMLHTTP')}
 18     ) || false;
 19   }
 20 }
 21 
 22 /**
 23  * From Prototype Library. 
 24  * 
 25  * @author Solo L(www.solol.org)
 26  */
 27 var Try = {
 28   these: function() {
 29     var returnValue;
 30 
 31     for (var i = 0, length = arguments.length; i < length; i++) {
 32       var lambda = arguments[i];
 33       try {
 34         returnValue = lambda();
 35         break;
 36       } catch (e) {}
 37     }
 38 
 39     return returnValue;
 40   }
 41 }
 42 
 43 /**
 44  * Request Object,now support IE and Firefox,ect.
 45  *
 46  * @author Solo L(www.solol.org)
 47  */
 48 var Request = function(){
 49 	this.request = Ajax.getTransport();
 50 }
 51 /**
 52  * Assigns destination URL, method, and other optional 
 53  * attributes of a pending request.
 54  * 
 55  * @param {Object} method
 56  * @param {Object} url
 57  * @param {Object} asyncFlag
 58  * @param {Object} userName
 59  * @param {Object} password
 60  * 
 61  * @author Solo L(www.solol.org)
 62  */
 63 Request["prototype"].open = function(method,url,asyncFlag,
 64 		userName,password){
 65 	return this.request.open(method,url,asyncFlag,userName,password);
 66 }
 67 
 68 /**
 69  * Transmits the request, optionally with postable string 
 70  * or DOM object data.
 71  * 
 72  * @param {Object} content
 73  * 
 74  * @author Solo L(www.solol.org)
 75  */
 76 Request["prototype"].send = function(content){
 77 	if(typeof content == "undefined"){
 78 		// for firefox
 79 		return this.request.send(null);
 80 	}
 81 	
 82 	return this.request.send(content);
 83 }
 84 /**
 85  * Stops the current request.
 86  * 
 87  * @author Solo L(www.solol.org)
 88  */
 89 Request["prototype"].abort = function(){
 90 	return this.request.abort();
 91 }
 92 /**
 93  * Returns complete set of headers (labels and values) 
 94  * as a string.
 95  * 
 96  * @author Solo L(www.solol.org)
 97  */
 98 Request["prototype"].getAllResponseHeaders = function(){
 99 	return this.request.getAllResponseHeaders();
100 }
101 /**
102  * Returns the string value of a single header label.
103  * 
104  * @author Solo L(www.solol.org)
105  */
106 Request["prototype"].getResponseHeader = function(headerLabel){
107 	return this.request.getResponseHeader(headerLabel);
108 }
109 /**
110  * Assigns a label/value pair to the header to be sent 
111  * with a request.
112  * 
113  * @param {Object} label
114  * @param {Object} value
115  * 
116  * @author Solo L(www.solol.org)
117  */
118 Request["prototype"].setRequestHeader = function(label, value){
119 	this.request.setRequestHeader(label,value);
120 }
121 
122 /**
123  * Object status integer:
124  * 
125  * 0 = uninitialized
126  * 1 = loading
127  * 2 = loaded
128  * 3 = interactive
129  * 4 = complete
130  * 
131  * @author Solo L(www.solol.org)
132  */
133 Request["prototype"].getReadyState = function(){
134 	return this.request.readyState;
135 }
136 /**
137  * Numeric code returned by server, such as 404 
138  * for "Not Found" or 200 for "OK".
139  * 
140  * @author Solo L(www.solol.org)
141  */
142 Request["prototype"].getStatus = function(){
143 	return this.request.status;
144 }
145 /**
146  * String message accompanying the status code.
147  * 
148  * @author Solo L(www.solol.org)
149  */
150 Request["prototype"].getStatusText = function(){
151 	return this.request.statusText;
152 }
153 /**
154  * String version of data returned from server process.
155  * 
156  * @author Solo L(www.solol.org)
157  */
158 Request["prototype"].getResponseText = function(){
159 	return this.request.responseText;
160 }
161 /**
162  * DOM-compatible document object of data returned 
163  * from server process.
164  * 
165  * @author Solo L(www.solol.org)
166  */
167 Request["prototype"].getResponseXML = function(){
168 	return this.request.responseXML;
169 }
170 
171 /**
172  * Set handler.
173  * 
174  * @author Solo L(www.solol.org)
175  */
176 Request["prototype"].setHandler = function(handler){
177 	this.request.onreadystatechange = handler;
178 }
179 
180 /**
181  * Helper method of Request Object for Ready State. 
182  * 
183  * @see Request.getReadyState()
184  * 
185  * @author Solo L(www.solol.org)
186  */
187 Request["prototype"].isUninitialized = function(){
188 	return this.request.readyState == 0;
189 }
190 Request["prototype"].isLoading = function(){
191 	return this.request.readyState == 1;
192 }
193 Request["prototype"].isLoaded = function(){
194 	return this.request.readyState == 2;
195 }
196 Request["prototype"].isInteractive = function(){
197 	return this.request.readyState == 3;
198 }
199 Request["prototype"].isComplete = function(){
200 	return this.request.readyState == 4;
201 }
202 
203 /**
204  * Helper method of Request Object for HTTP Status.
205  * 
206  * @author Solo L(www.solol.org)
207  */
208  Request["prototype"].isOk = function(){
209  	return this.request.status == 200;
210  }
211  Request["prototype"].isBadRequest = function(){
212  	return this.request.status == 400;
213  }
214  Request["prototype"].isUnauthorized = function(){
215  	return this.request.status == 401;
216  }
217  Request["prototype"].isForbidden = function(){
218  	return this.request.status == 403;
219  }
220  Request["prototype"].isNotFound = function(){
221  	return this.request.status == 404;
222  }
223  
224  /**
225  * Helper method of Request Object.
226  * 
227  * @author Solo L(www.solol.org)
228  */
229  Request["prototype"].doGet = function(url,asyncFlag,
230  			userName,password){
231  	this.request.open("GET",url,asyncFlag,userName,password);
232  	// for firefox
233  	this.request.send(null);
234  }
235  Request["prototype"].doPost = function(url,content,asyncFlag,
236  			userName,password){
237  	this.request.open("POST",url,asyncFlag,userName,password);
238  	// for firefox
239  	this.request.send(content);
240  }
241  Request["prototype"].doHead = function(url,asyncFlag,
242  			userName,password){
243  	this.request.open("HEAD",url,asyncFlag,userName,password);
244  	// for firefox
245  	this.request.send(null); 	
246  }
Copyright © SoloL.org 冀ICP备06003230号