UNPKG

laya-coreui-es

Version:

laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改

117 lines (116 loc) 4.07 kB
import { Event } from "../events/Event"; import { EventDispatcher } from "../events/EventDispatcher"; import { Utils } from "../utils/Utils"; import { Browser } from "../utils/Browser"; export class HttpRequest extends EventDispatcher { constructor() { super(...arguments); this._http = new XMLHttpRequest(); } send(url, data = null, method = "get", responseType = "text", headers = null) { this._responseType = responseType; this._data = null; if (Browser.onVVMiniGame || Browser.onQGMiniGame || Browser.onQQMiniGame || Browser.onAlipayMiniGame || Browser.onBLMiniGame || Browser.onHWMiniGame || Browser.onTTMiniGame || Browser.onTBMiniGame) { url = HttpRequest._urlEncode(url); } this._url = url; var _this = this; var http = this._http; http.open(method, url, true); let isJson = false; if (headers) { for (var i = 0; i < headers.length; i++) { http.setRequestHeader(headers[i++], headers[i]); } } else if (!(window.conch)) { if (!data || typeof (data) == 'string') http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); else { http.setRequestHeader("Content-Type", "application/json"); if (!(data instanceof ArrayBuffer) && typeof data !== "string") { isJson = true; } } } let restype = responseType !== "arraybuffer" ? "text" : "arraybuffer"; http.responseType = restype; if (http.dataType) { http.dataType = restype; } http.onerror = function (e) { _this._onError(e); }; http.onabort = function (e) { _this._onAbort(e); }; http.onprogress = function (e) { _this._onProgress(e); }; http.onload = function (e) { _this._onLoad(e); }; if (Browser.onBLMiniGame && Browser.onAndroid && !data) data = {}; http.send(isJson ? JSON.stringify(data) : data); } _onProgress(e) { if (e && e.lengthComputable) this.event(Event.PROGRESS, e.loaded / e.total); } _onAbort(e) { this.error("Request was aborted by user"); } _onError(e) { this.error("Request failed Status:" + this._http.status + " text:" + this._http.statusText); } _onLoad(e) { var http = this._http; var status = http.status !== undefined ? http.status : 200; if (status === 200 || status === 204 || status === 0) { this.complete(); } else { this.error("[" + http.status + "]" + http.statusText + ":" + http.responseURL); } } error(message) { this.clear(); console.warn(this.url, message); this.event(Event.ERROR, message); } complete() { this.clear(); var flag = true; try { if (this._responseType === "json") { this._data = JSON.parse(this._http.responseText); } else if (this._responseType === "xml") { this._data = Utils.parseXMLFromString(this._http.responseText); } else { this._data = this._http.response || this._http.responseText; } } catch (e) { flag = false; this.error(e.message); } flag && this.event(Event.COMPLETE, this._data instanceof Array ? [this._data] : this._data); } clear() { var http = this._http; http.onerror = http.onabort = http.onprogress = http.onload = null; } get url() { return this._url; } get data() { return this._data; } get http() { return this._http; } } HttpRequest._urlEncode = encodeURI;