原名JSPackager

源代码在线查看: request.js

软件大小: 4187 K
上传用户: yeling023
关键词: JSPackager
下载地址: 免注册下载 普通下载 VIP

相关代码

				function Request(url, options) {this.transport = new XMLHttpRequest();this.url = url;this.options = this.createOptions(options);var _this = this;this.onreadystatechange = function () {switch (_this.transport.readyState) {case 0:break;case 1:break;case 2:if (_this.onStart) {_this.onStart();}break;case 3:if (_this.onReceiving) {_this.onReceiving();}break;case 4:if (_this.isSuccess() == false) {if (_this.onFailure) {_this.onFailure();}} else {if (_this.onSuccess) {_this.onSuccess();}}if (_this.onFinish) {_this.onFinish();}this.free = true;_this.transport.onreadystatechange = function () {};break;}};this.free = true;}Request.prototype.send = function (params) {this.free = false;this.transport.onreadystatechange = this.onreadystatechange;var headers = this.options.headers;params = this.buildQueryString(params);if (this.options.method == "post") {this.transport.open(this.options.method, this.url, this.options.asynchronous);headers["Content-type"] = this.options.contentType;if (this.transport.overrideMimeType) {headers["Connection"] = "close";}} else {this.transport.open(this.options.method, this.url + (this.url.match(/\?/) ? "&" : "?") + params, this.options.asynchronous);}for (var n in headers) {this.transport.setRequestHeader(n, headers[n]);}this.transport.send(this.options.method == "post" ? params : null);return this;				};Request.prototype.isFree = function () {return this.free;};Request.prototype.createOptions = function () {var alias = {"asyn":"asynchronous"};function Creator(options) {this.headers = {"Accept":"'text/javascript, text/html, application/xml, text/xml, */*'"};for (var n in options) {var n = alias[n] || n;if (n == "headers") {var headers = options[n];for (var n in headers) {this.headers[n2] = headers[n2];}} else {this[n] = options[n];}}}Creator.prototype = {method:"get", asynchronous:false, contentType:"application/x-www-form-urlencoded"};return function (options) {return new Creator(options);};}();Request.prototype.isSuccess = function () {if (this.transport.status == null || this.transport.status == 0) {return null;} else {return this.transport.status >= 200 && this.transport.status < 300;}};Request.prototype.onStart = function () {};Request.prototype.setStartListener = function (listener) {this.onStart = listener;return this;};Request.prototype.onReceiving = function () {};Request.prototype.setReceivingListener = function (listener) {this.onReceiving = listener;return this;};Request.prototype.onSuccess = function () {};Request.prototype.setSuccessListener = function (listener) {this.onSuccess = listener;return this;};Request.prototype.onFailure = function () {				};Request.prototype.setFailureListener = function (listener) {this.onFailure = listener;return this;};Request.prototype.onFinish = function () {};Request.prototype.setFinishListener = function (listener) {this.onFinish = listener;return this;};Request.prototype.getHeader = function (name) {if (this.transport.readyState >= 3) {return this.transport.getResponseHeader(name);} else {$log.error("response not complete");}};Request.prototype.evalResult = function () {if (this.transport.readyState == 4) {return eval(this.transport.responseText);} else {$log.error("response not complete");}};Request.prototype.getResult = function () {				if (/\/xml/.test(this.getHeader("Content-Type"))) {return this.getXML();} else {return this.getText();}};Request.prototype.getXML = function () {if (this.transport.readyState == 4) {return this.transport.responseXML;} else {$log.error("response not complete");}};Request.prototype.getText = function () {if (this.transport.readyState >= 3) {return this.transport.responseText;} else {$log.error("response not complete");}};Request.prototype.buildQueryString = function (params) {if (typeof params == "string") {return params;} else {if (params instanceof Array) {return params.join("&");} else {if (params instanceof Object) {var paramStr = "";for (var n in params) {if (paramStr.length > 0) {paramStr += "&";}paramStr += encodeURIComponent(n) + "=" + encodeURIComponent(params[n]);}return paramStr;} else {return params;}}}};			

相关资源