axios链接带参数_Axios post 传参的params 与data 的两种形式【详解】[vue]

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进行修饰

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值