纯粹记录试错过程。
起因是想前端同时传递String变量、JSON对象,但是之前都是单纯传递参数或者对象。
1 前端request请求
wx.request({
url: "http://127.0.0.1:8080/superadmin/judgecheckresult?scanCode=" + that.data.scanCode, // 在 url 中传递 String
data: JSON.stringify(formData), // 将 formData 转化成JSON对象
method: 'POST',
header: {'Content-Type': 'application/json'}, // http 请求是 JSON 数据格式
success: function (res) {
console.log("res")
console.log(res)
}
})
HTTP Content-type 对照表 - 在线工具 - 开源中国社区
2 后台接收数据
@RequestMapping(value = "/judgecheckresult", method = RequestMethod.POST)
private Map<String, Object> judgeCheckResult(String scanCode, @RequestBody StuCheck stuCheck) { // @RequestBody 序列化前端的 JSON 对象
...
}
这里再说一下,后台处理数据也可以用@RequestParam String来接收数据,但是此时String不能为空,于是就用@RequestBody了。

本文探讨了在前后端交互中如何同时传递String变量和JSON对象,通过前端使用wx.request方法发送POST请求,并在后端通过@RequestBody注解接收数据,详细介绍了具体的实现步骤和技术要点。

409

被折叠的 条评论
为什么被折叠?



