美文网首页
axios 传递数组参数的方法

axios 传递数组参数的方法

作者: 佳瑞Jarrett | 来源:发表于2021-09-09 22:51 被阅读0次

axios不能传递数组参数,那么我们就通过以下方法来解决。
网上能提供千奇百怪的方法,但是我认为这个是最简单的。

let arr = [1,2,3]
axios({url: '/user/',
    params: {
        id: arr + '',    // 重点在这数组加上'',即可变为 "1,2,3"
    }
}

所形成的url为:/user?id=1,2,3 等价于 /user?id=1&id=2&id=3

后端接收参数的时候,用字符串类型进行接收,然后再转为列表。

@CrossOrigin
@GetMapping()
public ApiResult list(@RequestParam("companyId") Long companyId, @RequestParam("userId") String userList)
    {
        String str[] = userList.split(",");
        List<String> userNameList = Arrays.asList(str);
        return operationLogService.list(companyId, userNameList);
    }

看高招
https://www.jianshu.com/p/68d81da4e1ad
我尝试了链接里的方法,但是好像不管用。

如果您觉得上面的内容对您有帮助欢迎点赞、评论、转发!
更多内容请查阅作者博客:https://jiaruiblog.com
或者star作者github: https://github.com/Jarrettluo?tab=repositories

相关文章

网友评论

      本文标题:axios 传递数组参数的方法

      本文链接:https://www.haomeiwen.com/subject/ficvpltx.html