class HTTP { constructor() { this.xhr = new XMLHttpRequest(); } get(option) { return new Promise((success, error) => { var GetParams = "" for (const key in option.data) { GetParams += `&${key}=${option.data[key]}` } GetParams = GetParams.replace("&", "?") option.url += GetParams; this.xhr.open("GET", config.checkUrl() + option.url) this.xhr.send() this.onreadystatechange(success, error) }) } post(option) { return new Promise((success, error) => { this.xhr.open("POST", config.checkUrl() + option.url) this.xhr.setRequestHeader("Content-Type","application/json; charset = UTF-8"); this.xhr.send(JSON.stringify(option.data)) this.onreadystatechange(success, error) }) } onreadystatechange(success, error) { this.xhr.onreadystatechange = () => { if (this.xhr.readyState == 4) { if (this.xhr.status == 200) { var data = JSON.parse(this.xhr.responseText) switch (data.status) { case 200: return success(data.result || data.data) break; case 404: alert(data.msg); throw new Error(data.msg); break; } } else { console.log("暂无数据"); error(this.xhr) } } } } }