📄 html_ajax.js
字号:
return; } divpos = cont.indexOf(":", 1); if (divpos == -1) { this.raiseError("missing : for array", cont); return; } size = parseInt(cont.substring(0, divpos)); cont = cont.substring(divpos + 2); val = new Array(); if (cont.length < 1) { this.raiseError("array is too short", cont); return; } for (var i = 0; i < size; i++) { kret = this.unserialize(cont, 1); if (this.error || kret[0] == undefined || kret[1] == "") { this.raiseError("missing or invalid key, or missing value for array", cont); return; } vret = this.unserialize(kret[1], 1); if (this.error) { this.raiseError("invalid value for array", cont); return; } val[kret[0]] = vret[0]; cont = vret[1]; } if (cont.charAt(0) != "}") { this.raiseError("missing ending }, or too many values for array", cont); return; } rest = cont.substring(1); break; case "O": // object divpos = cont.indexOf(":"); if (divpos == -1) { this.raiseError("missing : for object", cont); return; } size = parseInt(cont.substring(0, divpos)); var objname = cont.substring(divpos + 2, divpos + 2 + size); if (cont.substring(divpos + 2 + size, divpos + 4 + size) != "\":") { this.raiseError("object name is too long, or missing \":", cont); return; } var objprops = this.unserialize("a:" + cont.substring(divpos + 4 + size), 1); if (this.error) { this.raiseError("invalid object properties", cont); return; } rest = objprops[1]; var objout = "function " + objname + "(){"; for (key in objprops[0]) { objout += "this." + key + "=objprops[0]['" + key + "'];"; } objout += "}val=new " + objname + "();"; eval(objout); break; default: this.raiseError("invalid input type", cont); } return (arguments.length == 1 ? val : [val, rest]); }, // }}} // {{{ getError /** * Gets the last error message * * @return string the last error message from unserialize() */ getError: function() { return this.message + "\n" + this.cont; }, // }}} // {{{ raiseError /** * Raises an eror (called by unserialize().) * * @param string message the error message * @param string cont the remaining unserialized content */ raiseError: function(message, cont) { this.error = 1; this.message = message; this.cont = cont; } // }}}}// }}}// Dispatcher.js/** * Class that is used by generated stubs to make actual AJAX calls * * @category HTML * @package AJAX * @author Joshua Eichorn <josh@bluga.net> * @copyright 2005 Joshua Eichorn * @license http://www.opensource.org/licenses/lgpl-license.php LGPL */function HTML_AJAX_Dispatcher(className,mode,callback,serverUrl,serializerType) { this.className = className; this.mode = mode; this.callback = callback; this.serializerType = serializerType; if (serverUrl) { this.serverUrl = serverUrl } else { this.serverUrl = window.location; }}HTML_AJAX_Dispatcher.prototype = { /** * Queue to use when making a request */ queue: 'default', /** * Timeout for async calls */ timeout: 20000, /** * Default request priority */ priority: 0, /** * Request options */ options: {}, /** * Make an ajax call * * @param string callName * @param Array args arguments to the report method */ doCall: function(callName,args) { var request = new HTML_AJAX_Request(); request.requestUrl = this.serverUrl; request.className = this.className; request.methodName = callName; request.timeout = this.timeout; request.contentType = this.contentType; request.serializer = eval('new HTML_AJAX_Serialize_'+this.serializerType); request.queue = this.queue; request.priority = this.priority; for(var i in this.options) { request[i] = this.options[i]; } for(var i=0; i < args.length; i++) { request.addArg(i,args[i]); }; if ( this.mode == "async" ) { request.isAsync = true; if (this.callback[callName]) { var self = this; request.callback = function(result) { self.callback[callName](result); } } } else { request.isAsync = false; } return HTML_AJAX.makeRequest(request); }, Sync: function() { this.mode = 'sync'; }, Async: function(callback) { this.mode = 'async'; if (callback) { this.callback = callback; } }};// HttpClient.js/** * XMLHttpRequest Wrapper * @category HTML * @package AJAX * @author Joshua Eichorn <josh@bluga.net> * @copyright 2005 Joshua Eichorn * @license http://www.opensource.org/licenses/lgpl-license.php LGPL */function HTML_AJAX_HttpClient() { }HTML_AJAX_HttpClient.prototype = { // request object request: null, // timeout id _timeoutId: null, callbackComplete: true, // has this request been aborted aborted: false, // method to initialize an xmlhttpclient init:function() { try { // Mozilla / Safari //this.xmlhttp = new HTML_AJAX_IframeXHR(); //uncomment these two lines to test iframe //return; this.xmlhttp = new XMLHttpRequest(); } catch (e) { // IE var XMLHTTP_IDS = new Array( 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ); var success = false; for (var i=0;i < XMLHTTP_IDS.length && !success; i++) { try { this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]); success = true; } catch (e) {} } if (!success) { try{ this.xmlhttp = new HTML_AJAX_IframeXHR(); this.request.iframe = true; } catch(e) { throw new Error('Unable to create XMLHttpRequest.'); } } } }, // check if there is a call in progress callInProgress: function() { switch ( this.xmlhttp.readyState ) { case 1: case 2: case 3: return true; break; default: return false; break; } }, // make the request defined in the request object makeRequest: function() { if (!this.xmlhttp) { this.init(); } try { if (this.request.Open) { this.request.Open(); } else if (HTML_AJAX.Open) { HTML_AJAX.Open(this.request); } if (this.request.multipart) { if (document.all) { this.iframe = true; } else { this.xmlhttp.multipart = true; } } // set onreadystatechange here since it will be reset after a completed call in Mozilla var self = this; this.xmlhttp.open(this.request.requestType,this.request.completeUrl(),this.request.isAsync); if (this.request.customHeaders) { for (i in this.request.customHeaders) { this.xmlhttp.setRequestHeader(i, this.request.customHeaders[i]); } } if (this.request.customHeaders && !this.request.customHeaders['Content-Type']) { var content = this.request.getContentType(); //opera is stupid for anything but plain text or xml!! if(window.opera && content != 'application/xml') { this.xmlhttp.setRequestHeader('Content-Type','text/plain; charset=utf-8'); this.xmlhttp.setRequestHeader('x-Content-Type', content + '; charset=utf-8'); } else { this.xmlhttp.setRequestHeader('Content-Type', content + '; charset=utf-8'); } } if (this.request.isAsync) { if (this.request.callback) { this.callbackComplete = false; } this.xmlhttp.onreadystatechange = function() { self._readyStateChangeCallback(); } } else { this.xmlhttp.onreadystatechange = function() {} } var payload = this.request.getSerializedPayload(); if (payload) { this.xmlhttp.setRequestHeader('Content-Length', payload.length); } this.xmlhttp.send(payload); if (!this.request.isAsync) { if ( this.xmlhttp.status == 200 ) { HTML_AJAX.requestComplete(this.request); if (this.request.Load) { this.request.Load(); } else if (HTML_AJAX.Load) { HTML_AJAX.Load(this.request); } return this._decodeResponse(); } else { var e = new Error('['+this.xmlhttp.status +'] '+this.xmlhttp.statusText); e.headers = this.xmlhttp.getAllResponseHeaders(); this._handleError(e); } } else { // setup timeout var self = this; this._timeoutId = window.setTimeout(function() { self.abort(true); },this.request.timeout); } } catch (e) { this._handleError(e); } }, // abort an inprogress request abort: function (automatic) { if (this.callInProgress()) { this.aborted = true; this.xmlhttp.abort(); if (automatic) { HTML_AJAX.requestComplete(this.request); this._handleError(new Error('Request Timed Out: time out was '+this.request.timeout+'ms')); } } }, // internal method used to handle ready state changes _readyStateChangeCallback:function() { try { switch(this.xmlhttp.readyState) { // XMLHTTPRequest.open() has just been called case 1: break; // XMLHTTPRequest.send() has just been called case 2: if (this.request.Send) { this.request.Send(); } else if (HTML_AJAX.Send) { HTML_AJAX.Send(this.request); } break; // Fetching response from server in progress case 3: if (this.request.Progress) { this.request.Progress(); } else if (HTML_AJAX.Progress ) { HTML_AJAX.Progress(this.request); } break; // Download complete case 4: window.clearTimeout(this._timeoutId); if (this.aborted) { if (this.request.Load) { this.request.Load(); } else if (HTML_AJAX.Load) { HTML_AJAX.Load(this.request); } } else if (this.xmlhttp.status == 200) { if (this.request.Load) { this.request.Load(); } else if (HTML_AJAX.Load ) { HTML_AJAX.Load(this.request); } var response = this._decodeResponse(); if (this.request.callback) { this.request.callback(response); this.callbackComplete = true; } } else { var e = new Error('HTTP Error Making Request: ['+this.xmlhttp.status+'] '+this.xmlhttp.statusText); this._handleError(e); } HTML_AJAX.requestComplete(this.request); break; } } catch (e) { this._handleError(e); } }, // decode response as needed _decodeResponse: function() { //try for x-Content-Type first var content = null; try { content = this.xmlhttp.getResponseHeader('X-Content-Type'); } catch(e) {} if(!content || content == null) { content = this.xmlhttp.getResponseHeader('Content-Type'); } //strip anything after ; if(content.indexOf(';') != -1) { content = content.substring(0, content.indexOf(';')); } // hook for xml, it doesn't need to be unserialized if(content == 'application/xml') { return this.xmlhttp.responseXML; } var unserializer = HTML_AJAX.serializerForEncoding(content); //alert(this.xmlhttp.getAllResponseHeaders()); // some sort of debug hook is needed here return unserializer.unserialize(this.xmlhttp.responseText); }, // handle sending an error where it needs to go _handleError: function(e) { HTML_AJAX.requestComplete(this.request,e); if (this.request.onError) { this.request.onError(e); } else if (HTML_AJAX.onError) { HTML_AJAX.onError(e,this.request); } else { throw e; } }}// Request.js/** * Class that contains everything needed to make a request * This includes: * The url were calling * If were calling a remote method, the class and method name * The payload, unserialized * The timeout for async calls * The callback method * Optional event handlers: onError, Load, Send * A serializer instance * * @category HTML * @package AJAX * @author Joshua Eichorn <josh@bluga.net> * @copyright 2005 Joshua Eichorn * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * * See Main.js for author/license details */function HTML_AJAX_Request(serializer) { this.serializer = serializer;}HTML_AJAX_Request.prototype = { // Instance of a serializer serializer: null, // Is this an async request isAsync: false, // HTTP verb requestType: 'POST',
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -