From 2beca068a4d92117b84413664bcbdd5c957fd6a6 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 8 May 2020 00:50:30 +0800 Subject: [PATCH] feat: complate deploy feature --- packages/common/src/http.ts | 2 +- packages/ployfill/src/xml-http-request.ts | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/common/src/http.ts b/packages/common/src/http.ts index bb7af2e0..f9f2ad8e 100644 --- a/packages/common/src/http.ts +++ b/packages/common/src/http.ts @@ -27,7 +27,7 @@ function request(config: RequestConfig) { for (const header in config.headers) { xhr.setRequestHeader(header, config.headers[header]); } - xhr.send(config.data); + xhr.send(typeof config.data === "string" ? config.data : JSON.stringify(config.data)); if ((xhr.getResponseHeader("Content-Type") + '').indexOf('application/json') != -1) { xhr.responseType = "json" } diff --git a/packages/ployfill/src/xml-http-request.ts b/packages/ployfill/src/xml-http-request.ts index 9ce15002..e2519cb2 100644 --- a/packages/ployfill/src/xml-http-request.ts +++ b/packages/ployfill/src/xml-http-request.ts @@ -151,6 +151,8 @@ export class XMLHttpRequest { this._mimeType = mimeType; } open(method: RequestMethod, url: string, async: boolean = true, user?: string, password?: string) { + if (this._readyState !== ReadyState.UNSENT) { throw new Error(`Error Status ${this._readyState}!`) } + this._method = method; this._url = url; this._async = async; @@ -195,7 +197,7 @@ export class XMLHttpRequest { this.onloadstart(); if (body) { let bodyType = Object.prototype.toString.call(body); - if (bodyType !== '[object String]') { throw new Error(`body(${bodyType}) must be string!`) } + if (typeof body !== "string") { throw new Error(`body(${bodyType}) must be string!`) } var out = this._connection.getOutputStream(); out.write(new JavaString(body).getBytes(UTF_8)); out.flush(); @@ -204,7 +206,6 @@ export class XMLHttpRequest { this.setReadyState(ReadyState.LOADING); this._status = this._connection.getResponseCode(); this._statusText = this._connection.getResponseMessage(); - this.setResponseHeaders(this._connection.getHeaderFields()); if (this._status >= 0 && this._status < 300) { this._response = this.readOutput(this._connection.getInputStream()); } else if (this._status >= 300 && this._status < 400) { @@ -212,6 +213,7 @@ export class XMLHttpRequest { } else { this._response = this.readOutput(this._connection.getErrorStream()); } + this.setResponseHeaders(this._connection.getHeaderFields()); this.onloadend(); } catch (ex) { if (ex instanceof SocketTimeoutException && this.ontimeout) { @@ -227,7 +229,6 @@ export class XMLHttpRequest { } private setResponseHeaders(header: any) { - this._responseHeaders = {}; header.forEach((key: string | number, value: string | any[]) => { this._responseHeaders[key] = value[value.length - 1] });