vue使用fetch.js发送post请求java后台无法获取参数值

问题:前台vue使用fetch.js发送post请求后,后台 request.getParameter()无法获取到参数值

思路:查阅后,原因为fetch中头文件Content-type这个Header为application/x-www-form-urlencoded导致request请求中的form data变成request payload

处理办法:后台controller中使用流接受数据后,在进行查询操作既可。

  • vue代码
  • /**
     * 获取行业大类
     */
    export const hangyebrief = industryId => fetch('/console/good/industry/findIndustry', {
      industryId: 2
    }, 'POST');
    

    controller代码

  •     @RequestMapping("findIndustry")
        @ResponseBody
        public AjaxRes findIndustry( HttpServletRequest request,HttpServletResponse response){
            StringBuilder sb = new StringBuilder();
            try(BufferedReader reader = request.getReader();) {
                char[]buff = new char[1024];
                int len;
                while((len = reader.read(buff)) != -1) {
                    sb.append(buff,0, len);
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
            String idString = sb.toString();
            String  industryId = idString.substring(idString.indexOf(":")+1,idString.indexOf("}"));
            AjaxRes aj = new AjaxRes();
            GoodIndustryVo goodIndustry  = new GoodIndustryVo();
            if (industryId != null && industryId != "") {
                goodIndustry.setIndustryId(Integer.parseInt(industryId));
            }
            List<GoodIndustryVo> goodIndustryList = goodIndustryService.find(goodIndustry);
            aj.setObj(goodIndustryList);
            return aj;
        }
    

      

      

posted on 2018-09-10 17:11 Schon_zh 阅读() 评论() 编辑 收藏

版权声明:本文为schon原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/schon/p/9621073.html