vue【axios】get/post请求params/data传参总结

axios中get/post请求方式
1. 前言
最近突然发现post请求可以使用params方式传值,然后想总结一下其中的用法。
2.1 分类
在这里插入图片描述
get请求中没有data传值方式
2.2 get请求
params
基础类型接收,名字对应即可

// method
const params = {
    id: '123456789',
    name: '张三'
}
test(params)

// api
export function test (params) {
  return axios({
    url: url,
    method: 'GET',
    params: params
  })
}

// 后台
@GetMapping("/test")
public Result test(Long id, String name) {
    return Res.ok();
}

使用Map接收,需要添加 RequestParam 注解

// method
const params = {
    id: '123456789',
    name: '张三'
}
test(params)

// api
export function test (params) {
  return axios({
    url: url,
    method: 'GET',
    params: params
  })
}

// 后台
@GetMapping("/test")
public Result test(@RequestParam Map<String, Object> map) {
    return Res.ok();
}

使用实体类接收

// 实体类
@Data
public class TestEntity {
    Long id;
    String name;
}

// method
const params = {
    id: '123456789',
    name: '张三'
}
test(params)

// api
export function test (params) {
  return axios({
    url: url,
    method: 'GET',	
    params: params
  })
}

// 后台
@GetMapping("/test")
public Result test(TestEntity testEntity) {
    return Res.ok();
}

ps: get请求不允许传递List,需要使用qs插件或者配置axios,具体参考链接
2.3 post请求
2.3.1 params 与 get方式相同
与get相似,基础类型接收,名字对应即可

// method
const params = {
    id: '123456789',
    name: '张三'
}
test(params)

// api
export function test (params) {
  return axios({
    url: url,
    method: 'POST',
    params: params
  })
}

// 后台
@PostMapping("/test")
public Result test(Long id, String name) {
    return Res.ok();
}

与get相似,使用map接收

// method
const params = {
    id: '123456789',
    name: '张三'
}
test(params)

// api
export function test (params) {
  return axios({
    url: url,
    method: 'POST',
    params: params
  })
}

// 后台
@PostMapping("/test")
public Result test(@RequestParam Map<String, Object> map) {
    return Res.ok();
}

与get相似,使用实体类接收

// 实体类
@Data
public class TestEntity {
    Long id;
    String name;
}

// method
const params = {
    id: '123456789',
    name: '张三'
}
test(params)

// api
export function test (params) {
  return axios({
    url: url,
    method: 'POST',	
    params: params
  })
}

// 后台
@PostMapping("/test")
public Result test(TestEntity testEntity) {
    return Res.ok();
}

2.3.2 data
使用实体类接收

// 实体类
@Data
public class TestEntity {
    Long id;
    String name;
}

// method
const params = {
    id: '123456789',
    name: '张三'
}
test(params)

// api
export function test (params) {
  return axios({
    url: url,
    method: 'POST',	
    data: params
  })
}

@PostMapping("/test")
public Result test(@RequestBody TestEntity testEntity) {
    return Res.ok();
}

4. 总结
总体来说,只要使用 params get与post请求基本是一样使用的,如果参数名与传递名称不一致,需要使用@RequestParam修饰,若使用Map接收参数,必须使用@RequestParam修饰。但是如果想传list类型的数据,需要使用单独的方法处理(参考链接)。
若使用data传递参数,必须使用一个实体类接收参数,而且需要添加注解@RequestBody进行修饰。

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
vueaxios库提供了很多对于发送HTTP请求方法,其中包括get和post请求。虽然它们在用法上有一些细微的区别,但它们都可以用来向服务器发送请求,获取响应数据。 首先,无论是get请求还是post请求,我们都需要引入axios库并创建一个axios实例。然后我们可以使用该实例来发送请求。不论是get请求还是post请求,我们都需要提供一个URL作为请求的目标地址。 在get请求中,我们可以将参数作为URL的一部分,参数可以通过params选项进行设置。例如,我们可以使用params选项传递一些查询参数。 ```javascript axios.get('/api/user', { params: { id: 1 } }).then(response => { console.log(response.data); }).catch(error => { console.error(error); }); ``` 在post请求中,我们需要使用data选项将要发送的数据传递给服务器。例如,我们可以使用data选项将一个JavaScript对象发送给服务器。 ```javascript axios.post('/api/user', { id: 1, name: 'John' }).then(response => { console.log(response.data); }).catch(error => { console.error(error); }); ``` 无论是get请求还是post请求,我们都可以使用then方法来处理成功的响应,并使用catch方法来处理错误的响应。 总的来说,对于vue中的axios库来说,get请求post请求在用法上是相似的。它们都需要一个URL作为请求的目标地址,并可以使用paramsdata选项来传递参数。区别主要在于get请求将参数作为URL的一部分,而post请求将参数放在请求体中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李泽举

如对你有帮助,那我就没白写

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值