vue使用fetch.js發送post請求java後臺無法獲取參數值
阿新 • • 發佈:2018-09-10
type brush rip true logs trace 查詢 servle service
問題:前臺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; }
vue使用fetch.js發送post請求java後臺無法獲取參數值