Skip to content

Commit c003e02

Browse files
committed
fix(h5): H5 request API 对 post 请求参数做序列化
1 parent 55ccf62 commit c003e02

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/taro-h5/src/api/request/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { serializeParams } from '../utils'
44

55
function generateRequestUrlWithParams (url, params) {
66
params = typeof params === 'string' ? params : serializeParams(params)
7-
url += (~url.indexOf('?') ? '&' : '?') + `${params}`
7+
url += (~url.indexOf('?') ? '&' : '?') + params
88
url = url.replace('?&', '?')
99
return url
1010
}
@@ -45,6 +45,15 @@ export default function request (options) {
4545
params.cache = options.cache || 'default'
4646
if (methodUpper === 'GET' || methodUpper === 'HEAD') {
4747
url = generateRequestUrlWithParams(url, options.data)
48+
} else if (methodUpper === 'POST') {
49+
let contentType = options.header && (options.header['Content-Type'] || options.header['content-type'])
50+
if (contentType === 'application/json') {
51+
params.body = JSON.stringify(options.data)
52+
} else if (contentType === 'application/x-www-form-urlencoded') {
53+
params.body = serializeParams(options.data)
54+
} else {
55+
params.body = options.data
56+
}
4857
} else {
4958
params.body = options.data
5059
}

packages/taro-h5/src/api/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function serializeParams (params) {
4545
return ''
4646
}
4747
return Object.keys(params)
48-
.map(item => (`${item}=${enc(params[item])}`)).join('&')
48+
.map(key => (`${enc(key)}=${enc(params[key])}`)).join('&')
4949
}
5050

5151
export {

0 commit comments

Comments
 (0)