当前位置: 首页 > 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)

相关文章:

  • 左支座零件的机械加工工艺规程及工艺装备设计【计算机辅助设计与制造CAD】
  • YOLOv8 加持 MobileNetv3,目标检测新篇章
  • docker的基本使用以及使用Docker 运行D435i
  • Notepad+正则表达式使用方法
  • SpringCloud 微服务全栈体系(十四)
  • Android 13 - Media框架(14)- OpenMax(二)
  • python django 小程序图书借阅源码
  • C#入门(2): namespace、类
  • 计算机毕业设计选题推荐-点餐微信小程序/安卓APP-项目实战
  • 蓝桥杯单片机综合练习——工厂灯光控制
  • 【开源】基于Vue.js的在线课程教学系统的设计和实现
  • STM32框架之按键扫描新思路
  • linux gdb调试
  • 23.11.19日总结
  • 六大排序(插入排序、希尔排序、冒泡排序、选择排序、堆排序、快速排序)未完
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • FineReport中如何实现自动滚屏效果
  • HomeBrew常规使用教程
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • Javascript编码规范
  • JavaScript设计模式之工厂模式
  • leetcode386. Lexicographical Numbers
  • PV统计优化设计
  • 前端知识点整理(待续)
  • 树莓派 - 使用须知
  • 通过调用文摘列表API获取文摘
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (8)STL算法之替换
  • (二)学习JVM —— 垃圾回收机制
  • (十三)Java springcloud B2B2C o2o多用户商城 springcloud架构 - SSO单点登录之OAuth2.0 根据token获取用户信息(4)...
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (转)EOS中账户、钱包和密钥的关系
  • (转)Google的Objective-C编码规范
  • (转)Linq学习笔记
  • (转)自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版
  • (转载)VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)
  • .L0CK3D来袭:如何保护您的数据免受致命攻击
  • .NET Core 版本不支持的问题
  • .Net Memory Profiler的使用举例
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .net 调用php,php 调用.net com组件 --
  • .NET 发展历程
  • .NET 分布式技术比较
  • .NET开源项目介绍及资源推荐:数据持久层 (微软MVP写作)
  • .NET牛人应该知道些什么(2):中级.NET开发人员
  • @GlobalLock注解作用与原理解析
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • [Android]How to use FFmpeg to decode Android f...
  • [BZOJ4010]菜肴制作
  • [C++] sqlite3_get_table 的使用
  • [codeforces]Checkpoints
  • [LeetCode] 196. 删除重复的电子邮箱