当前位置: 首页 > news >正文

Axios传值的几种方式

 <body><script src="https://unpkg.com/axios/dist/axios.min.js"></script></body>

axios基本使用

默认是get请求

注意:get请求无请求体,可以有body,但是不建议带

使用get方式进行无参请求

<script>axios({url:'http://localhost:8080/get/getAll',method:'get'}).then(res=>{console.log(res.data.data)})</script>@GetMapping("/get/getAll")public ResResult getAllUser(){List<User> list = userService.list();return ResResult.okResult(list);}

 使用get方式请求,参数值直接放在路径中

 

<script>axios({url:'http://localhost:8080/get/1',method:'get'}).then(res=>{console.log(res.data.data)})</script>后端接口@GetMapping("/get/{id}")public ResResult getUserById(@PathVariable("id") Long id){User user = userService.getById(id);return ResResult.okResult(user);}

 使用get方式请求,参数拼接在路径中:方式① 

<script>axios({url:'http://localhost:8080/get?id=1',method:'get'}).then(res=>{console.log(res.data.data)})</script>后端接口@GetMapping("/get")public ResResult getUserByIds(@RequestParam("id") Long id){User user = userService.getById(id);return ResResult.okResult(user);}

 使用get方式请求,参数拼接在路径中:方式②

<script>axios({url:'http://localhost:8080/get',params:{id:'2'},method:'get'}).then(res=>{console.log(res.data.data)})</script>
后端接口
@GetMapping("/get")public ResResult getUserByIds(@RequestParam("id") Long id){User user = userService.getById(id);return ResResult.okResult(user);
}

使用get方式请求,拼接多个参数在路径中:方式③ 

<script>axios({url:'http://localhost:8080/get',params:{id:'2',username:'swx'},method:'get'}).then(res=>{console.log(res.data.data)})
</script>
后端接口
@GetMapping("/get")public ResResult getUserByIds(@RequestParam("id") Long id,@RequestParam("username") String username){LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();wrapper.eq(User::getUsername,username);wrapper.eq(User::getId,id);User user = userService.getOne(wrapper);return ResResult.okResult(user);}

 post请求接收json格式数据

<script>axios({url:'http://localhost:8080/post/test',data:{'username':'swx'},method:'post'}).then(res=>{console.log(res.data.data)})
</script>
后端接口
@PostMapping("/post/test")public ResResult getUserByIdPostTest(@RequestBody User user){LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();wrapper.eq(User::getUsername,user.getUsername());User users = userService.getOne(wrapper);return ResResult.okResult(users);}

3、请求简写方式&请求失败处理 

get无参请求

<script>axios.get('http://localhost:8080/get/getAll').then(res=>{console.log(res.data.data)}).catch(err=>{console.log('timeout')console.log(err)})
</script>

get有参请求,post方式不可以这样请求

<script>axios.get('http://localhost:8080/get',{params:{id:'2',username:'swx'}}).then(res=>{console.log(res.data.data)}).catch(err=>{console.log('timeout')console.log(err)})
</script>

 post有参请求,以json格式请求

<script>axios.post('http://localhost:8080/post',"id=2&username=swx").then(res=>{console.log(res.data.data)}).catch(err=>{console.log('timeout')console.log(err)})
</script>也可以一下方式,但是后端要加@RequestBody注解
<script>axios.post('http://localhost:8080/post/test',{username:'swx'}).then(res=>{console.log(res.data.data)}).catch(err=>{console.log('timeout')console.log(err)})
</script>

axios并发请求

<script>axios.all([axios.get('http://localhost:8080/get/getAll'),axios.get('http://localhost:8080/get/get',{params:{id:'1'}})]).then(res=>{//返回的是数组,请求成功返回的数组console.log(res[0].data.data),console.log(res[1].data.data)}).catch(err=>{console.log(err)})
</script>
后端接口
@GetMapping("/get/getAll")public ResResult getAllUser(){List<User> list = userService.list();return ResResult.okResult(list);}@GetMapping("/get/get")public ResResult getUserByIdt(@RequestParam("id") Long id){User user = userService.getById(id);return ResResult.okResult(user);}

 方式2:使用spread方法处理返回的数组

<script>axios.all([axios.get('http://localhost:8080/get/getAll'),axios.get('http://localhost:8080/get/get',{params:{id:'1'}})]).then(//高端一些axios.spread((res1,res2)=>{console.log(res1.data.data),console.log(res2.data.data)})).catch(err=>{console.log(err)})
</script>

axios全局配置

<script>axios.defaults.baseURL='http://localhost:8080'; //全局配置属性axios.defaults.timeout=5000; //设置超时时间//发送请求axios.get('get/getAll').then(res=>{console.log(res.data.data)});axios.post('post/getAll').then(res=>{console.log(res.data.data)});
</script>

axios实例 

<script>//创建实例let request = axios.create({baseURL:'http://localhost:8080',timeout:5000});//使用实例request({url:'get/getAll'}).then(res=>{console.log(res.data.data)});request({url:'post/getAll',method:'post'}).then(res=>{console.log(res.data.data)})
</script>

Axios各种参数携带方式详解 - 知乎 (zhihu.com)

http://www.lryc.cn/news/236625.html

相关文章:

  • git pull 报错 error object file is empty , The remote end hung up unexpectedly
  • 手机数码类展示预约小程序效果如何
  • 图神经网络:消息传递算法
  • 安全+Linux!IBM新一代大型机Z14全新发布
  • Java中的局部变量和成员变量的区别
  • 基于C++实现循环赛日程表(分治算法)
  • 基于uni-app的汽车租赁app的设计与实现
  • 3.8-镜像的发布
  • Navicat 基于 GaussDB 主备版的快速入门
  • String的字符串拼接
  • 反渗透水处理成套设备有哪些
  • DPC15 国产带有 SPI 接口的独立 CAN 控制器兼容替代MCP2551
  • 【ELK01】ELK简介以及ElasticSearch安装、ES客户端工具-Head安装、报错问题整理
  • 根据音频绘制频谱
  • SSL证书对网站SEO的好处
  • YB506AB是一款理电池充、放电管理专用芯片,集成锂电池充电管理和降压DC-DC电路。
  • Linux | C语言中volatile关键字的理解
  • 汇编层面有三个主要的操作对象
  • React中的Redux:简介和实例代码
  • Modbus转Profinet网关在金银精炼控制系统中应用案例
  • 小程序商城免费搭建之java商城 电子商务Spring Cloud+Spring Boot+二次开发+mybatis+MQ+VR全景+b2b2c
  • Rabin加解密算法(python3)
  • 【带头学C++】----- 七、链表 ---- 7.5 学生管理系统(链表--上)
  • (四)什么是Vite——冷启动时vite做了什么(源码、middlewares)
  • Docker部署MinIO对象存储服务器结合Cpolar实现远程访问
  • C#入门(1):程序结构、数据类型
  • Scala---元组
  • 【Linux】冯诺依曼体系结构、操作系统、进程概念、进程状态、环境变量、进程地址空间
  • 【hive-解决】HiveAccessControlException Permission denied: CREATEFUNCTION
  • 内网穿透的应用-通过内网穿透快速搭建公网可访问的Spring Boot接口调试环境