[ Index ] |
|
Code source de Seagull 0.6.1 |
1 /** 2 * XMLHttpRequest Wrapper 3 * @category HTML 4 * @package AJAX 5 * @author Joshua Eichorn <josh@bluga.net> 6 * @copyright 2005 Joshua Eichorn 7 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 8 */ 9 function HTML_AJAX_HttpClient() { } 10 HTML_AJAX_HttpClient.prototype = { 11 // request object 12 request: null, 13 14 // timeout id 15 _timeoutId: null, 16 17 callbackComplete: true, 18 19 // has this request been aborted 20 aborted: false, 21 22 // method to initialize an xmlhttpclient 23 init:function() 24 { 25 try { 26 // Mozilla / Safari 27 //this.xmlhttp = new HTML_AJAX_IframeXHR(); //uncomment these two lines to test iframe 28 //return; 29 this.xmlhttp = new XMLHttpRequest(); 30 } catch (e) { 31 // IE 32 var XMLHTTP_IDS = new Array( 33 'MSXML2.XMLHTTP.5.0', 34 'MSXML2.XMLHTTP.4.0', 35 'MSXML2.XMLHTTP.3.0', 36 'MSXML2.XMLHTTP', 37 'Microsoft.XMLHTTP' ); 38 var success = false; 39 for (var i=0;i < XMLHTTP_IDS.length && !success; i++) { 40 try { 41 this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]); 42 success = true; 43 } catch (e) {} 44 } 45 if (!success) { 46 try{ 47 this.xmlhttp = new HTML_AJAX_IframeXHR(); 48 this.request.iframe = true; 49 } catch(e) { 50 throw new Error('Unable to create XMLHttpRequest.'); 51 } 52 } 53 } 54 }, 55 56 // check if there is a call in progress 57 callInProgress: function() 58 { 59 switch ( this.xmlhttp.readyState ) { 60 case 1: 61 case 2: 62 case 3: 63 return true; 64 break; 65 default: 66 return false; 67 break; 68 } 69 }, 70 71 // make the request defined in the request object 72 makeRequest: function() 73 { 74 if (!this.xmlhttp) { 75 this.init(); 76 } 77 78 try { 79 if (this.request.Open) { 80 this.request.Open(); 81 } 82 else if (HTML_AJAX.Open) { 83 HTML_AJAX.Open(this.request); 84 } 85 86 if (this.request.multipart) { 87 if (document.all) { 88 this.iframe = true; 89 } else { 90 this.xmlhttp.multipart = true; 91 } 92 } 93 94 // set onreadystatechange here since it will be reset after a completed call in Mozilla 95 var self = this; 96 this.xmlhttp.open(this.request.requestType,this.request.completeUrl(),this.request.isAsync); 97 if (this.request.customHeaders) { 98 for (i in this.request.customHeaders) { 99 this.xmlhttp.setRequestHeader(i, this.request.customHeaders[i]); 100 } 101 } 102 if (this.request.customHeaders && !this.request.customHeaders['Content-Type']) { 103 var content = this.request.getContentType(); 104 //opera is stupid for anything but plain text or xml!! 105 if(window.opera && content != 'application/xml') 106 { 107 this.xmlhttp.setRequestHeader('Content-Type','text/plain; charset=utf-8'); 108 this.xmlhttp.setRequestHeader('x-Content-Type', content + '; charset=utf-8'); 109 } 110 else 111 { 112 this.xmlhttp.setRequestHeader('Content-Type', content + '; charset=utf-8'); 113 } 114 } 115 116 if (this.request.isAsync) { 117 if (this.request.callback) { 118 this.callbackComplete = false; 119 } 120 this.xmlhttp.onreadystatechange = function() { self._readyStateChangeCallback(); } 121 } else { 122 this.xmlhttp.onreadystatechange = function() {} 123 } 124 var payload = this.request.getSerializedPayload(); 125 if (payload) { 126 this.xmlhttp.setRequestHeader('Content-Length', payload.length); 127 } 128 this.xmlhttp.send(payload); 129 130 if (!this.request.isAsync) { 131 if ( this.xmlhttp.status == 200 ) { 132 HTML_AJAX.requestComplete(this.request); 133 if (this.request.Load) { 134 this.request.Load(); 135 } else if (HTML_AJAX.Load) { 136 HTML_AJAX.Load(this.request); 137 } 138 139 return this._decodeResponse(); 140 } else { 141 var e = new Error('['+this.xmlhttp.status +'] '+this.xmlhttp.statusText); 142 e.headers = this.xmlhttp.getAllResponseHeaders(); 143 this._handleError(e); 144 } 145 } 146 else { 147 // setup timeout 148 var self = this; 149 this._timeoutId = window.setTimeout(function() { self.abort(true); },this.request.timeout); 150 } 151 } catch (e) { 152 this._handleError(e); 153 } 154 }, 155 156 // abort an inprogress request 157 abort: function (automatic) 158 { 159 if (this.callInProgress()) { 160 this.aborted = true; 161 this.xmlhttp.abort(); 162 163 if (automatic) { 164 HTML_AJAX.requestComplete(this.request); 165 this._handleError(new Error('Request Timed Out: time out was '+this.request.timeout+'ms')); 166 } 167 } 168 }, 169 170 // internal method used to handle ready state changes 171 _readyStateChangeCallback:function() 172 { 173 try { 174 switch(this.xmlhttp.readyState) { 175 // XMLHTTPRequest.open() has just been called 176 case 1: 177 break; 178 // XMLHTTPRequest.send() has just been called 179 case 2: 180 if (this.request.Send) { 181 this.request.Send(); 182 } else if (HTML_AJAX.Send) { 183 HTML_AJAX.Send(this.request); 184 } 185 break; 186 // Fetching response from server in progress 187 case 3: 188 if (this.request.Progress) { 189 this.request.Progress(); 190 } else if (HTML_AJAX.Progress ) { 191 HTML_AJAX.Progress(this.request); 192 } 193 break; 194 // Download complete 195 case 4: 196 window.clearTimeout(this._timeoutId); 197 if (this.aborted) { 198 if (this.request.Load) { 199 this.request.Load(); 200 } else if (HTML_AJAX.Load) { 201 HTML_AJAX.Load(this.request); 202 } 203 } 204 else if (this.xmlhttp.status == 200) { 205 if (this.request.Load) { 206 this.request.Load(); 207 } else if (HTML_AJAX.Load ) { 208 HTML_AJAX.Load(this.request); 209 } 210 211 var response = this._decodeResponse(); 212 213 if (this.request.callback) { 214 this.request.callback(response); 215 this.callbackComplete = true; 216 } 217 } 218 else { 219 var e = new Error('HTTP Error Making Request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText); 220 this._handleError(e); 221 } 222 HTML_AJAX.requestComplete(this.request); 223 break; 224 } 225 } catch (e) { 226 this._handleError(e); 227 } 228 }, 229 230 // decode response as needed 231 _decodeResponse: function() { 232 //try for x-Content-Type first 233 var content = null; 234 try { 235 content = this.xmlhttp.getResponseHeader('X-Content-Type'); 236 } catch(e) {} 237 if(!content || content == null) 238 { 239 content = this.xmlhttp.getResponseHeader('Content-Type'); 240 } 241 //strip anything after ; 242 if(content.indexOf(';') != -1) 243 { 244 content = content.substring(0, content.indexOf(';')); 245 } 246 // hook for xml, it doesn't need to be unserialized 247 if(content == 'application/xml') 248 { 249 return this.xmlhttp.responseXML; 250 } 251 var unserializer = HTML_AJAX.serializerForEncoding(content); 252 //alert(this.xmlhttp.getAllResponseHeaders()); // some sort of debug hook is needed here 253 return unserializer.unserialize(this.xmlhttp.responseText); 254 }, 255 256 // handle sending an error where it needs to go 257 _handleError: function(e) 258 { 259 HTML_AJAX.requestComplete(this.request,e); 260 if (this.request.onError) { 261 this.request.onError(e); 262 } else if (HTML_AJAX.onError) { 263 HTML_AJAX.onError(e,this.request); 264 } 265 else { 266 throw e; 267 } 268 } 269 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 01:27:52 2007 | par Balluche grâce à PHPXref 0.7 |