文章目录
Axios发送的post请求,后端拿不到参数解决方法
原因: Axios请求,数据以json方式发送 Content-Type: application/json
,后端使用getParameter()是拿不到参数的
解决方法: 可以在绑定数据时候,使用axios自带库Qs
的stringify
将对象转换为 param =value¶m = value样式
这样Axios发送请求时,Content-Type就变成了application/x-www-form-urlencoded
后端使用@RequestParam
或者形参对应参数名
获取数据
import qs from 'Qs'
this.$axios({
method:"post",
url:"/login",
data:qs.stringify(this.ruleForm)
}).then(function (resp){
console.log("请求成功",resp)
}).catch(function(err){
console.log("请求失败",err)
})
@RequestBody和@RequestParam的区别
@RequestBody获取请求体中的数据
,只能有一个,可以用字符串接收也可以是实体接收,看例子:
一、 Post
请求
Content-Type=application/json
请求体中为Json
1.使用字符串接收请求体
2.使用实体类接收
二、Post`请求
Content-Type=application/x-www-form-urlencoded
请求体中为 键=值&键=值 (和表单提交应该是一样的)