返回顶部

vue开发中使用axios的post、get方法传递参数

问题

  • 前端在使用 axios.post()携带参数强求后端Controller接口时,不使用实体类接收,导致接收的参数为 null 。为了搞清楚有哪些用法,也是研究了一下。
GET请求
  • 方式一:参数格式:{params:{key1:val1, key2:val2}}
    //前端
    this.$axios.get("http://localhost:8080/api/function/login/loginget", 
	{params:{name:"carlget1", password:"password"}})

    // 后端代码
    @RequestMapping("loginget")
    public Map<String, Object> login(String name, String password){}
  • 方式二:RestFul风格
    //前端
    axios.get("/checkitem/getItemIdByGroupId/"+row.id)

    //后端
    @GetMapping("/getItemIdByGroupId/{id}")
    public List<Integer> getItemIdByGroupId(@PathVariable("id") Integer id){}
POST请求
  • 方式一:实体类接收
    //前端
    var param = {
        currentPage: this.pagination.currentPage, //页码
        pageSize: this.pagination.pageSize,  //每页显示的记录数
        queryString: this.pagination.queryString  //查询条件
    };
    axios.post("/checkitem/groupPageQuery",param)

    //后端
    @PostMapping("/groupPageQuery")
    public PageResult groupPageQuery(@RequestBody QueryPageBean queryPageBean){}

  • 方式二:实体类+RestFul风格接收
    //前端
    axios.post("/checkitem/addGroup/"+this.checkitemIds,this.formData)

    //后端
    @PostMapping("/updateGroup/{checkitemIds}")
    public Result updateGroup(@PathVariable("checkitemIds") Integer[] checkitemIds, @RequestBody CheckGroup checkGroup){

总结下来,最好还是使用 实体类 接收参数

posted @ 2021-07-30 16:34  凑数的园丁  阅读(2510)  评论(0编辑  收藏  举报