fix: some bug at xhr and console
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
d9e2b50953
commit
e8d41e8a43
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user