1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > uni.request POST 请求后台接收不到参数

uni.request POST 请求后台接收不到参数

时间:2020-08-31 13:33:00

相关推荐

uni.request POST 请求后台接收不到参数

uni.request POST 请求后台接收不到参数

问题描述

前端用uni.reauest发送POST请求,后台接收不到参数。

原因分析

是因为uni.request对传入data数据转换后,后台接收数据形式没有对应上。

uniapp 官网对data的说明:

默认的content-type

也就是说,当methodPOST,并且headerapplication/json时,data会被序列化为 JSON 字符串,所以这个时候,后台需要用一个对应的封装好的实体类或者Map配合注解@RequestBody接收。

前端请求,methodPOSTcontent-type默认,即application/josn

uni.request({url: 'http://localhost:8080/login/wxLogin',method: 'POST',data: {code},success: (res) => {console.log(res)}})

后端需要用@RequestBod Map<String, String>接收:

@PostMapping("/wxLogin")public RespBean wxLogin(@RequestBody Map<String, String> map) {System.out.println("code: " + map.get("code"));return RespBean.ok("成功");}

解决办法

如果不想像上面那样用 map 接收,可以修改uni.requestheader'content-type': 'application/x-www-form-urlencoded'

前端请求:

uni.request({url: 'http://localhost:8080/login/wxLogin',method: 'POST',header: {'content-type': 'application/x-www-form-urlencoded'},data: {code},success: (res) => {console.log(res)}})

后端直接接收:

@PostMapping("/wxLogin")public RespBean wxLogin(String code) {System.out.println("code: " + code);return RespBean.ok("成功");}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。