fix: some bug at xhr and console

Signed-off-by: MiaoWoo <admin@yumc.pw>
clean
MiaoWoo 2020-02-23 22:33:51 +08:00
parent d9e2b50953
commit e8d41e8a43
3 changed files with 17 additions and 13 deletions

View File

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

View File

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

View File

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