今天使用微信小程序的post请求,发现服务器无法获取到请求的参数。
在微信开发者工具查看请求,发现携带有参数,但是服务器却没有参数。
用postman测试了服务器的接口,发现接口没问题,可以获取到参数。
后来发现是编码的问题,只需要在请求头加上,utf-8是否加好像都无所谓,不过最好加一下。
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
wx.request({
url: url,
header:{
authorization:token,
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
data: {
userName: this.data.userName,
},
method: 'post',
dataType: 'json',
responseType: 'text',
success: function(res) {
console.log(res.data.key)
},
fail: function(res) {
console.log(res)
},
complete: function(res) {
console.log(res)
},
})
},