服务器接收不了微信小程序的数据

使用primose封装微信小程序
发送数据给服务器, 服务器接收不了数据 -为空,
之前header:{
// 'content-type': 'application/json', // 默认值
}
改为
header: {
'content-type': 'application/x-www-form-urlencoded',
},
就没有问题了。
 
wx.request({
url: url,
method: method ? method : 'GET',
header: {
'content-type': 'application/x-www-form-urlencoded',
},
data:data.data,
success: function (res) {
if (res.statusCode < 500) {
resolve(res.data)
} else {
showError()
reject(res.data)
}
},

其中原因:官网:

  • 对于 POST 方法且 header['content-type'] 为 application/json 的数据,会对数据进行 JSON 序列化
  • 对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换成 query string (encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)

猜你喜欢

转载自www.cnblogs.com/wen-/p/12194011.html