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){
总结下来,最好还是使用 实体类 接收参数
作 者:凑数的园丁
出 处:https://www.cnblogs.com/lq-404/
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主: 如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。