今天爱分享给大家带来Axios post 传参的params 与data 的两种形式【详解】[vue],希望能够帮助到大家。
问题描述
params是添加到url的请求字符串中的,一般用于get请求。
data是添加到请求体(body)中的, 一般用于post请求。
上面,只是一般情况. 其实,post请求也可以使用params方式传值 , 但是get请求没有data方式,
本文就来介绍一下post的两种传值方式
Axios 中posts的params与data的两种传参
第一种:data方式
this.$axios({
url: '/api/user/login' ,
method: 'post',
headers: {
'Content-Type': 'application/json'
},
data:{
username: this.user,
pwd: this.pwd
}
}).then((res) => {
console.log(res)
})
第二种:params方式
this.$axios({
url: '/api/user/login' ,
method: 'post',
headers: {
'Content-Type': 'application/json'
},
params:{
username: this.user,
pwd: this.pwd
}
}).then((res) => {
console.log(res)
})
注:直接使用post方法,传递的参数为 data 的方式
代码如下:
this.$axios.post('/api/user/login',{username: this.user, pwd: this.pwd }),
{
headers: {
'Content-Type': 'application/json'
}
}).then((res) => {
console.log(res)
使用场景
一般,情况下都是使用 data 的传参方式,但有时会发现需要使用 params 的方式,后台才能获取到数据,这与后台的接口有关,我们前端不能控制。如Java接口
若使用 Map 接收参数,必须使用 @RequestParam 修饰。但是如果想传list类型的数据,需要使用单独的方法处理(参考链接)。
若使用 data 传递参数,必须使用一个实体类接收参数,而且需要添加注解 @RequestBody 进行修饰
原文链接:http://blog.itblood.com/447.html,转载请注明出处。

![TS勇者劳拉的雌狱 挂载AI汉化版[新汉化][1.3G] [RPG/汉化]](/wp-content/uploads/replace/2025/09/11/5b33aa2662cecb0ec0ba555307c59f40.webp)