正常情况下,Axios中post请求传参的params与data的两种形式
params是添加到url的请求字符串中的,用于get请求,。
data是添加到请求体(body)中的, 用于post请求。
其实,post请求也可以使用params方式传值 , 但是get请求没有data方式。
本文就来介绍一下post的两种传值方式
Axios 中posts的params与data的两种传参
第一种:data方式
this.$axios({
url:\n'/api/user/login' ,
method:\n'post',
headers:\n{
'Content-Type': 'application/json'
},
data:{
username:\nthis.user,
pwd:\nthis.pwd
}
}).then((res) => {
console.log(res)
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
})
第二种:params方式
this.$axios({
url:\n'/api/user/login' ,
method:\n'post',
headers:\n{
'Content-Type': 'application/json'
},
params:{
username:\nthis.user,
pwd:\nthis.pwd
}
}).then((res) => {
console.log(res)
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
})
注:直接使用post方法,默认传递的参数为 data 的方式
代码如下:
this.$axios.post('/api/user/login',{username:\nthis.user, pwd: this.pwd }),
{
headers:\n{
'Content-Type': 'application/json'
}
}).then((res)\n=> {
\n\n\n\n\n\n\n\n\n\n\n\n
console.log(res)
}
用post请求的时候可以同时用data跟params,当然跟后端接受有关系,总的来说,就是params就是参数拼接的方式,data就是在body里面的方式。
post请求使用params的场景可能比较少,大部分情况还是data方式。
比如接入springboot security权限,formlogin的登陆接口就是需要通过params方式获取参数。