📄 connection.js
字号:
/*
* Ext JS Library 2.0
* Copyright(c) 2006-2007, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/*Copyright (c) 2007, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 2.2.0*//** * The Connection Manager provides a simplified interface to the XMLHttpRequest * object. It handles cross-browser instantiantion of XMLHttpRequest, negotiates the * interactive states and server response, returning the results to a pre-defined * callback you create. * * @namespace YAHOO.util * @module connection * @requires yahoo *//** * The Connection Manager singleton provides methods for creating and managing * asynchronous transactions. * * @class Connect */YAHOO.util.Connect ={ /** * @description Array of MSFT ActiveX ids for XMLHttpRequest. * @property _msxml_progid * @private * @static * @type array */ _msxml_progid:[ 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ], /** * @description Object literal of HTTP header(s) * @property _http_header * @private * @static * @type object */ _http_headers:{}, /** * @description Determines if HTTP headers are set. * @property _has_http_headers * @private * @static * @type boolean */ _has_http_headers:false, /** * @description Determines if a default header of * Content-Type of 'application/x-www-form-urlencoded' * will be added to any client HTTP headers sent for POST * transactions. * @property _use_default_post_header * @private * @static * @type boolean */ _use_default_post_header:true, /** * @description Determines if a default header of * Content-Type of 'application/x-www-form-urlencoded' * will be added to client HTTP headers sent for POST * transactions. * @property _default_post_header * @private * @static * @type boolean */ _default_post_header:'application/x-www-form-urlencoded', /** * @description Determines if a default header of * 'X-Requested-With: XMLHttpRequest' * will be added to each transaction. * @property _use_default_xhr_header * @private * @static * @type boolean */ _use_default_xhr_header:true, /** * @description The default header value for the label * "X-Requested-With". This is sent with each * transaction, by default, to identify the * request as being made by YUI Connection Manager. * @property _default_xhr_header * @private * @static * @type boolean */ _default_xhr_header:'XMLHttpRequest', /** * @description Determines if custom, default headers * are set for each transaction. * @property _has_default_header * @private * @static * @type boolean */ _has_default_headers:true, /** * @description Determines if custom, default headers * are set for each transaction. * @property _has_default_header * @private * @static * @type boolean */ _default_headers:{}, /** * @description Property modified by setForm() to determine if the data * should be submitted as an HTML form. * @property _isFormSubmit * @private * @static * @type boolean */ _isFormSubmit:false, /** * @description Property modified by setForm() to determine if a file(s) * upload is expected. * @property _isFileUpload * @private * @static * @type boolean */ _isFileUpload:false, /** * @description Property modified by setForm() to set a reference to the HTML * form node if the desired action is file upload. * @property _formNode * @private * @static * @type object */ _formNode:null, /** * @description Property modified by setForm() to set the HTML form data * for each transaction. * @property _sFormData * @private * @static * @type string */ _sFormData:null, /** * @description Collection of polling references to the polling mechanism in handleReadyState. * @property _poll * @private * @static * @type object */ _poll:{}, /** * @description Queue of timeout values for each transaction callback with a defined timeout value. * @property _timeOut * @private * @static * @type object */ _timeOut:{}, /** * @description The polling frequency, in milliseconds, for HandleReadyState. * when attempting to determine a transaction's XHR readyState. * The default is 50 milliseconds. * @property _polling_interval * @private * @static * @type int */ _polling_interval:50, /** * @description A transaction counter that increments the transaction id for each transaction. * @property _transaction_id * @private * @static * @type int */ _transaction_id:0, /** * @description Member to add an ActiveX id to the existing xml_progid array. * In the event(unlikely) a new ActiveX id is introduced, it can be added * without internal code modifications. * @method setProgId * @public * @static * @param {string} id The ActiveX id to be added to initialize the XHR object. * @return void */ setProgId:function(id) { this._msxml_progid.unshift(id); }, /** * @description Member to enable or disable the default POST header. * @method setDefaultPostHeader * @public * @static * @param {boolean} b Set and use default header - true or false . * @return void */ setDefaultPostHeader:function(b) { this._use_default_post_header = b; }, /** * @description Member to enable or disable the default POST header. * @method setDefaultXhrHeader * @public * @static * @param {boolean} b Set and use default header - true or false . * @return void */ setDefaultXhrHeader:function(b) { this._use_default_xhr_header = b; }, /** * @description Member to modify the default polling interval. * @method setPollingInterval * @public * @static * @param {int} i The polling interval in milliseconds. * @return void */ setPollingInterval:function(i) { if(typeof i == 'number' && isFinite(i)){ this._polling_interval = i; } }, /** * @description Instantiates a XMLHttpRequest object and returns an object with two properties: * the XMLHttpRequest instance and the transaction id. * @method createXhrObject * @private * @static * @param {int} transactionId Property containing the transaction id for this transaction. * @return object */ createXhrObject:function(transactionId) { var obj,http; try { // Instantiates XMLHttpRequest in non-IE browsers and assigns to http. http = new XMLHttpRequest(); // Object literal with http and tId properties obj = { conn:http, tId:transactionId }; } catch(e) { for(var i=0; i<this._msxml_progid.length; ++i){ try { // Instantiates XMLHttpRequest for IE and assign to http. http = new ActiveXObject(this._msxml_progid[i]); // Object literal with conn and tId properties obj = { conn:http, tId:transactionId }; break; } catch(e){} } } finally { return obj; } }, /** * @description This method is called by asyncRequest to create a * valid connection object for the transaction. It also passes a * transaction id and increments the transaction id counter. * @method getConnectionObject * @private * @static * @return {object} */ getConnectionObject:function() { var o; var tId = this._transaction_id; try { o = this.createXhrObject(tId); if(o){ this._transaction_id++; } } catch(e){} finally { return o; } }, /** * @description Method for initiating an asynchronous request via the XHR object. * @method asyncRequest * @public * @static * @param {string} method HTTP transaction method * @param {string} uri Fully qualified path of resource * @param {callback} callback User-defined callback function or object * @param {string} postData POST body * @return {object} Returns the connection object */ asyncRequest:function(method, uri, callback, postData) { var o = this.getConnectionObject(); if(!o){ return null; } else{ if(this._isFormSubmit){ if(this._isFileUpload){ this.uploadFile(o.tId, callback, uri, postData); this.releaseObject(o); return; } //If the specified HTTP method is GET, setForm() will return an //encoded string that is concatenated to the uri to //create a querystring. if(method.toUpperCase() == 'GET'){ if(this._sFormData.length != 0){ // If the URI already contains a querystring, append an ampersand // and then concatenate _sFormData to the URI. uri += ((uri.indexOf('?') == -1)?'?':'&') + this._sFormData; } else{ uri += "?" + this._sFormData; } } else if(method.toUpperCase() == 'POST'){ //If POST data exist in addition to the HTML form data, //it will be concatenated to the form data. postData = postData?this._sFormData + "&" + postData:this._sFormData; } } o.conn.open(method, uri, true); if(this._use_default_xhr_header){ if(!this._default_headers['X-Requested-With']){ this.initHeader('X-Requested-With', this._default_xhr_header, true); } } if(this._isFormSubmit || (postData && this._use_default_post_header)){ this.initHeader('Content-Type', this._default_post_header); if(this._isFormSubmit){ this.resetFormState(); } } if(this._has_default_headers || this._has_http_headers){ this.setHeader(o); } this.handleReadyState(o, callback); o.conn.send(postData || null); return o; } }, /** * @description This method serves as a timer that polls the XHR object's readyState * property during a transaction, instead of binding a callback to the * onreadystatechange event. Upon readyState 4, handleTransactionResponse * will process the response, and the timer will be cleared. * @method handleReadyState * @private * @static * @param {object} o The connection object * @param {callback} callback The user-defined callback object * @return {void} */ handleReadyState:function(o, callback) { var oConn = this; if(callback && callback.timeout){ this._timeOut[o.tId] = window.setTimeout(function(){ oConn.abort(o, callback, true); }, callback.timeout); } this._poll[o.tId] = window.setInterval( function(){ if(o.conn && o.conn.readyState == 4){ window.clearInterval(oConn._poll[o.tId]); delete oConn._poll[o.tId]; if(callback && callback.timeout){ delete oConn._timeOut[o.tId]; } oConn.handleTransactionResponse(o, callback); } } ,this._polling_interval); }, /** * @description This method attempts to interpret the server response and * determine whether the transaction was successful, or if an error or * exception was encountered. * @method handleTransactionResponse * @private * @static * @param {object} o The connection object * @param {object} callback The sser-defined callback object * @param {boolean} isAbort Determines if the transaction was aborted. * @return {void} */ handleTransactionResponse:function(o, callback, isAbort) { // If no valid callback is provided, then do not process any callback handling. if(!callback){ this.releaseObject(o); return; } var httpStatus, responseObject; try { if(o.conn.status !== undefined && o.conn.status != 0){ httpStatus = o.conn.status; } else{ httpStatus = 13030; } } catch(e){ // 13030 is the custom code to indicate the condition -- in Mozilla/FF -- // when the o object's status and statusText properties are // unavailable, and a query attempt throws an exception. httpStatus = 13030; } if(httpStatus >= 200 && httpStatus < 300){ responseObject = this.createResponseObject(o, callback.argument); if(callback.success){ if(!callback.scope){ callback.success(responseObject); } else{ // If a scope property is defined, the callback will be fired from // the context of the object. callback.success.apply(callback.scope, [responseObject]); } } } else{ switch(httpStatus){ // The following cases are wininet.dll error codes that may be encountered. case 12002: // Server timeout case 12029: // 12029 to 12031 correspond to dropped connections. case 12030: case 12031: case 12152: // Connection closed by server. case 13030: // See above comments for variable status. responseObject = this.createExceptionObject(o.tId, callback.argument, (isAbort?isAbort:false)); if(callback.failure){ if(!callback.scope){ callback.failure(responseObject); } else{ callback.failure.apply(callback.scope, [responseObject]); } } break; default: responseObject = this.createResponseObject(o, callback.argument); if(callback.failure){ if(!callback.scope){ callback.failure(responseObject); } else{ callback.failure.apply(callback.scope, [responseObject]); } } } } this.releaseObject(o); responseObject = null; }, /** * @description This method evaluates the server response, creates and returns the results via * its properties. Success and failure cases will differ in the response
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -