关于axios的多种请求多种后端接收方式
目录
关于axios的多种请求多种后端接收方式
在现有的项目中,后台的数据传输有多种格式。
1.针对单个的检索条件,一般只有2-3个参数,后端不愿意封装成class,只能前端单个传输
method = RequestMethod.GET
@ApiParam(value = "页数", required = true) @RequestParam Integer pageNum,
复制代码
2.针对form表单提交,数据较多,后端又要求class传输
method = RequestMethod.POST
@ApiParam(value = "申请退货对象", required = true) @RequestBody SubmitReturnedBo submitReturnedBo)
复制代码
3.后端有了以上两个要求还是不能满足,既有单个传输,同时需要class
method = RequestMethod.POST
@ApiParam(value = "申请退货对象", required = true) @RequestBody SubmitReturnedBo submitReturnedBo)
@ApiParam(value = "页数", required = true) @RequestParam Integer pageNum,
复制代码
针对以上请求,前端是vue全家桶的项目,异步请求方案是用axios进行请求
一开始是想使用json数据传输格式
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'复制代码
在java端接受的时候,后端觉得字段太多,以下的方式接受又嫌麻烦,通过JSONObject需要多个getString
@RequestMapping(value = “/servehotselectiveajax”)
@ResponseBody public int servehotselectiveajax(HttpServletRequest req,HttpServletResponse resp,@RequestBody JSONObject obj)
无奈,现在使用的默认的传输格式
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'复制代码
要求1方案
要求2方案
要求3方案
完毕~
转载于:https://juejin.im/post/5b8f4e706fb9a05cd972ddf4