开发微信小程序所遇问题
1.发送post请求后台接收不到数据
1.背景:在开发小程序过程中,想要使用wx.request发送一个注册对象到后台并插入数据库。使用postman测试后台接口没有问题,但是使用小程序发送后台就一直接收不到数据。
postman截图
后台注册方法截图
前端页面代码
// An highlighted block
registerUser(){
wx.request({
url:"http://localhost:9001/commonUser/wx/register",
header: {
"Content-Type": "application/json"
},
data:{
user:this.$data.user
},
method:'POST',
})
}
- 原因:前端代码存在2个问题
-
1.后台使用@RequestBody接收的是Json字符串,而你前台传过去的参数是一个Json对象,需要使用Json对象转换成Json字符串。就像postman截图请求的参数一样
-
2.传递的对象不正确,多了个user,只需要user后面花括号的内容就行
2.解决
- 1.使用JSON.stringify() 解决第一个问题
- 2.修改传递参数data的定义
registerUser(){
wx.request({
url:"http://localhost:9001/commonUser/wx/register",
header: {
"Content-Type": "application/json"
},
data: JSON.stringify(this.$data.user),
method:'POST',
})
}