Skip to content

Commit

Permalink
fix(h5): unify the default content-type (#7796)
Browse files Browse the repository at this point in the history
* fix(h5): unify the default content-type

* fix(style): remove trailing spaces

* fix(h5): improve the solution

* fix(h5): update test case

Co-authored-by: Yue, Zongkun <zyue@microstrategy.com>
  • Loading branch information
yuezk and Yue, Zongkun authored Oct 9, 2020
1 parent 60bb8fc commit 89e0ef0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 3 additions & 4 deletions packages/taro-h5/__test__/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 6 additions & 3 deletions packages/taro-h5/src/api/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ 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) {
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)
} 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
Expand Down

0 comments on commit 89e0ef0

Please sign in to comment.