Engine.WebRequest = class { constructor() { this.data = {}; this.responseText = ""; this.callback = null; this.xhr = null; this.onProgress = null; } makeRequest(url, callback) { this.callback = callback; this.url = url; this.xhr = new XMLHttpRequest(); // set the request type and url this.xhr.open('GET', this.url, true); this.xhr.responseType = "text"; this.xhr.addEventListener('error', (event) => { this.onError(event) }, false); this.xhr.addEventListener('abort', (event) => { this.onAbort(event) }, false); this.xhr.addEventListener('progress', (event) => { this.onProgressEvent(event) }, false); this.xhr.addEventListener('load', (event) => { this.onLoad(event) }, false); this.xhr.send(); } makePostRequest(url, params, callback) { this.callback = callback; this.xhr = new XMLHttpRequest(); this.xhr.open("POST", url, true); //Send the proper header information along with the request this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); this.xhr.responseType = "text"; this.xhr.addEventListener('error', (event) => { this.onError(event) }, false); this.xhr.addEventListener('abort', (event) => { this.onAbort(event) }, false); this.xhr.addEventListener('progress', (event) => { this.onProgressEvent(event) }, false); this.xhr.addEventListener('load', (event) => { this.onLoad(event) }, false); this.xhr.send(params); } onError(event) { Debug.LogError("Error with request"); } onAbort(event) { Debug.LogError("Request aborted!"); } onProgressEvent(event) { if (this.onProgress != null) this.onProgress(this, event.loaded, event.total); } onLoad(event) { Debug.Log("Request completed!"); var status = typeof this.xhr.status === 'undefined' ? 200 : this.xhr.status; if (status == 200) { this.responseText = this.xhr.responseText; try { this.data = JSON.parse(this.responseText); } catch (e) { } } if (this.callback) this.callback(); } abort() { this.xhr.abort(); } }