fix: some bug at xhr and console

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-02-23 22:33:51 +08:00
parent 9c4543df30
commit 45cb6fa667
3 changed files with 17 additions and 13 deletions

View File

@ -80,7 +80,7 @@ export class MiaoScriptConsole implements Console {
} }
object(obj) { object(obj) {
for (var i in obj) { for (var i in obj) {
this.logger(i, '=>', obj[i]) this.info(i, '=>', obj[i])
} }
} }
ex(ex: Error) { ex(ex: Error) {

View File

@ -24,8 +24,11 @@ function request(config: RequestConfig) {
for (const header in config.headers) { for (const header in config.headers) {
xhr.setRequestHeader(header, config.headers[header]); xhr.setRequestHeader(header, config.headers[header]);
} }
let future = xhr.send(config.data); xhr.send(config.data);
return future.get(); if ((xhr.getResponseHeader("Content-Type") + '').indexOf('application/json') != -1) {
xhr.responseType = "json"
}
return xhr.get();
} }
function _proxy(method: Method) { function _proxy(method: Method) {

View File

@ -114,7 +114,7 @@ export class XMLHttpRequest {
return this._statusText; return this._statusText;
} }
get response() { get response() {
return JSON.parse(this._response); return this._response ? JSON.parse(this._response) : this._response;
} }
get responseText() { get responseText() {
return this._response; return this._response;
@ -174,6 +174,16 @@ export class XMLHttpRequest {
if (!this._async) { future.get() } if (!this._async) { future.get() }
return future; return future;
} }
get() {
switch (this._responseType) {
case "json":
return this.response;
case "text":
return this.responseText;
default:
throw Error(`Unsupport ResponseType: ${this._responseType} !`)
}
}
abort() { abort() {
this._connection.disconnect(); this._connection.disconnect();
this.onabort(); this.onabort();
@ -182,7 +192,6 @@ export class XMLHttpRequest {
private _send(body?: string | object) { private _send(body?: string | object) {
try { try {
this._connection.connect(); this._connection.connect();
this.onloadstart(); this.onloadstart();
if (body) { if (body) {
let bodyType = Object.prototype.toString.call(body); let bodyType = Object.prototype.toString.call(body);
@ -204,14 +213,6 @@ export class XMLHttpRequest {
this._response = this.readOutput(this._connection.getErrorStream()); this._response = this.readOutput(this._connection.getErrorStream());
} }
this.onloadend(); this.onloadend();
switch (this._responseType) {
case "json":
return this.response;
case "text":
return this.responseText;
default:
throw Error(`Unsupport ResponseType: ${this._responseType} !`)
}
} catch (ex) { } catch (ex) {
if (ex instanceof SocketTimeoutException && this.ontimeout) { if (ex instanceof SocketTimeoutException && this.ontimeout) {
return this.ontimeout(ex) return this.ontimeout(ex)