前后端分离数组传递问题(springboot)(Vue)

前后端分离数组传递问题

昨天与前端对接时,我后端需要List的数据,就是找不到参数,我看了前端代码也没发现问题。绝

问题解决过程

我的后端代码:

    @Transactional
    @PostMapping("/backstage/modity")
    public CommonResult moditys(@RequestParam("img") List<String> img , @RequestParam("id") Integer id){
        if(!modity(img , id)){
            return CommonResult.failed();
        }
        return CommonResult.success("上传轮播图成功");
    }

因为我不是搞前端的只知道他的传参代码是这个样子的

data:{
img,
id
}

然后我查看网页的传参的数据样子是

id:1,
img[0]:https://....,
img[1]:https://....,

我恍然大悟于是我做了这个测试

    @Transactional
    @PostMapping("/backstage/modity")
    public CommonResult moditys(@RequestParam("img[0]") String img , @RequestParam("id") Integer id){
        System.out.println(img);
        return CommonResult.success("上传轮播图成功");
    }

果不其然打印:https://.....

解决方法

1,前端操作:你可先将[]和""去除,然后用split(",")将它变为数组。
2,前端操作:先转JSON然后用正则表达式去除[]和"".

这里后端不好操作因为你并不知道数组大小,不可能写img[0],img[1]....,傻子行为。

posted @ 2022-10-20 10:50  意难平呀!  阅读(63)  评论(0)    收藏  举报