From 05e6d3e01514389399c79c8122fec700c5da4ced Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Thu, 8 Oct 2020 17:22:13 +0800 Subject: [PATCH 1/4] fix(h5): unify the default content-type --- packages/taro-h5/src/api/request/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/taro-h5/src/api/request/index.js b/packages/taro-h5/src/api/request/index.js index eb11ccb1e1e6..4160e453eb08 100644 --- a/packages/taro-h5/src/api/request/index.js +++ b/packages/taro-h5/src/api/request/index.js @@ -52,10 +52,11 @@ function _request (options) { if (methodUpper === 'GET' || methodUpper === 'HEAD') { url = generateRequestUrlWithParams(url, options.data) } else if (typeof options.data === 'object') { - const contentType = options.header && (options.header['Content-Type'] || options.header['content-type']) - if (contentType && contentType.indexOf('application/json') >= 0) { + const contentType = (options.header && (options.header['Content-Type'] || options.header['content-type'])) || 'application/json' + + if (contentType.indexOf('application/json') >= 0) { params.body = JSON.stringify(options.data) - } else if (contentType && contentType.indexOf('application/x-www-form-urlencoded') >= 0) { + } else if (contentType.indexOf('application/x-www-form-urlencoded') >= 0) { params.body = serializeParams(options.data) } else { params.body = options.data From 2ea40be4a77246483678df1b1bcdcb097dfab14f Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Thu, 8 Oct 2020 17:43:45 +0800 Subject: [PATCH 2/4] fix(style): remove trailing spaces --- packages/taro-h5/src/api/request/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/taro-h5/src/api/request/index.js b/packages/taro-h5/src/api/request/index.js index 4160e453eb08..9572de8ced23 100644 --- a/packages/taro-h5/src/api/request/index.js +++ b/packages/taro-h5/src/api/request/index.js @@ -53,7 +53,7 @@ function _request (options) { url = generateRequestUrlWithParams(url, options.data) } else if (typeof options.data === 'object') { const contentType = (options.header && (options.header['Content-Type'] || options.header['content-type'])) || 'application/json' - + if (contentType.indexOf('application/json') >= 0) { params.body = JSON.stringify(options.data) } else if (contentType.indexOf('application/x-www-form-urlencoded') >= 0) { From d8d10eb97351bf7bd5aada1f1f3d9cacda6b4019 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Fri, 9 Oct 2020 10:21:42 +0800 Subject: [PATCH 3/4] fix(h5): improve the solution --- packages/taro-h5/src/api/request/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/taro-h5/src/api/request/index.js b/packages/taro-h5/src/api/request/index.js index 9572de8ced23..388ffaa79912 100644 --- a/packages/taro-h5/src/api/request/index.js +++ b/packages/taro-h5/src/api/request/index.js @@ -52,7 +52,9 @@ function _request (options) { if (methodUpper === 'GET' || methodUpper === 'HEAD') { url = generateRequestUrlWithParams(url, options.data) } else if (typeof options.data === 'object') { - const contentType = (options.header && (options.header['Content-Type'] || options.header['content-type'])) || 'application/json' + options.header = options.header || {}; + options.header['Content-Type'] = options.header['Content-Type'] || options.header['content-type'] || 'application/json' + const contentType = options.header['Content-Type']; if (contentType.indexOf('application/json') >= 0) { params.body = JSON.stringify(options.data) From 14621b0369596d26c85ed7b097daac0ed27840e2 Mon Sep 17 00:00:00 2001 From: "Yue, Zongkun" Date: Fri, 9 Oct 2020 12:31:48 +0800 Subject: [PATCH 4/4] fix(h5): update test case --- packages/taro-h5/__test__/request-test.js | 7 +++---- packages/taro-h5/src/api/request/index.js | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/taro-h5/__test__/request-test.js b/packages/taro-h5/__test__/request-test.js index 46e1bbf3b0bb..1a77552f9223 100644 --- a/packages/taro-h5/__test__/request-test.js +++ b/packages/taro-h5/__test__/request-test.js @@ -73,11 +73,10 @@ describe('request', () => { expect(fetch.mock.calls[0][0]).toBe('https://github.com') expect(fetch.mock.calls[0][1]).toEqual({ method: 'POST', - body: { - arg: 123 - }, + body: JSON.stringify({ arg: 123 }), headers: { - 'A': 'CCC' + 'A': 'CCC', + 'Content-Type': 'application/json' }, mode: 'cors', cache: 'no-cache', diff --git a/packages/taro-h5/src/api/request/index.js b/packages/taro-h5/src/api/request/index.js index 388ffaa79912..f67a79bce87c 100644 --- a/packages/taro-h5/src/api/request/index.js +++ b/packages/taro-h5/src/api/request/index.js @@ -52,9 +52,9 @@ function _request (options) { if (methodUpper === 'GET' || methodUpper === 'HEAD') { url = generateRequestUrlWithParams(url, options.data) } else if (typeof options.data === 'object') { - options.header = options.header || {}; + options.header = options.header || {} options.header['Content-Type'] = options.header['Content-Type'] || options.header['content-type'] || 'application/json' - const contentType = options.header['Content-Type']; + const contentType = options.header['Content-Type'] if (contentType.indexOf('application/json') >= 0) { params.body = JSON.stringify(options.data)