解决Vue使用axios传递数组到SpringBoot后台

首先为了解决这个bug测试了几十遍,不断的重启测试,最终,终于找到了解决方案

  1. 使用最笨的方法,将数组里面的值,在循环里拼接在URL后面

  2. 第二种方法就是使用QS

第一种这里就不说了,大家自行去测试即可,这里主要收一下第二种方法,因为看了很多网上的方案,都说的很含糊,导致会产生很多bug

  • 首先可以在自己的vue项目里安装QS(也可以不用安装,有些库它自带,如果怕出现问题最好还是安装i一下)

  • 安装:npm install qs 即可

  • 然后在组件页面的<script>标签里导入即可,import QS from 'qs',也可以在main.js里导入(全局导入)

希望大家测试的时候先跟我写的一模一样,成功后再修改自行去测试,避免走弯路!!!

这里提供一下后台的接口

     @RequestMapping("/delete")
     @ResponseBody
     public String deleteById(@RequestParam(value = "id[]") Integer[] id) {
         System.out.println(id.length);
         String str = "";
         for (Integer integer : id) {
             str += integer;
         }
         return str;
     }

这里只是简单的接受数组,然后返回给前端看能不能接收,一般我们用来做批量删除

注意形参列表,务必要写成

@RequestParam(value = "id[]") Integer[] id

前端请求

 this.axios({
         method: "post",
         url: "http://localhost:8081/delete",
         data: QS.stringify({'id': [1, 2, 3, 4]}, {arrayFormat: 'brackets'})
       }).then(resp => {
         alert(resp.data);
       });

注意:请求方式必须是 post

大功告成!!!

补充:

arrayFormat的取值有三种:brackets,repeat,indices

indices:解析后 -> id[0]=1 & id[1]=2 & id[2]=3

brackets:解析后 -> id[]=1 & aid[]=2 & id[]=3

repeat:解析后 -> id=1 & id=2 & id=3

对于参数匹配后端接口方式,请参考:qs的arrayFormat与SpringBoot接收数组问题_蚍蜉叹的博客-CSDN博客

  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,我来为你回答这个问题。 首先,在Vue项目中引入axios需要先安装axios,可以使用以下命令: ``` npm install axios --save ``` 然后在需要使用axiosVue组件中引入axios: ``` import axios from 'axios' ``` 接下来,我们可以使用axios后台发送请求,以下是一个示例: ``` axios.get('/api/users').then(response => { console.log(response.data) }) ``` 上述代码向后台发送了一个GET请求,请求的路径是`/api/users`,并在请求成功后打印了响应数据。 接下来,我们需要后台提供相应的接口来完成增删改查操作。这里以Spring Boot为例,我们可以使用Spring Boot提供的注解来定义RESTful API接口。以下是一个示例: ``` @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserService userService; @GetMapping public List<User> getUsers() { return userService.getUsers(); } @PostMapping public void addUser(@RequestBody User user) { userService.addUser(user); } @PutMapping("/{id}") public void updateUser(@PathVariable Long id, @RequestBody User user) { userService.updateUser(id, user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } } ``` 上述代码中,我们定义了一个`UserController`类,该类提供了四个接口,分别对应了获取用户列表、添加用户、更新用户和删除用户四个操作。其中,`@GetMapping`、`@PostMapping`、`@PutMapping`和`@DeleteMapping`分别对应了HTTP协议中的GET、POST、PUT和DELETE请求。 最后,我们可以在Vue项目中使用axios来调用这些接口,以完成增删改查操作。以下是一个示例: ``` // 获取用户列表 axios.get('/api/users').then(response => { console.log(response.data) }) // 添加用户 axios.post('/api/users', { name: '张三', age: 18, gender: '男' }).then(response => { console.log(response.data) }) // 更新用户 axios.put('/api/users/1', { name: '李四', age: 20, gender: '女' }).then(response => { console.log(response.data) }) // 删除用户 axios.delete('/api/users/1').then(response => { console.log(response.data) }) ``` 上述代码分别调用了四个接口,以完成增删改查操作。其中,`axios.post`、`axios.put`和`axios.delete`中的第二个参数分别为要添加、更新和删除的用户数据。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值