一、问题描述
controller响应给前端的数据中包含中文,前端解析所得显示结果出现乱码。
二、解决方法
1、无返回值
@RequestMapping("ajaxReq") public void ajaxReq(HttpServletRequest req,HttpServletResponse resp) throws IOException{ resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); String username = req.getParameter("username"); String password = req.getParameter("password");
resp.getWriter().write(username+" : "+password); }
2、有返回值
@RequestMapping(value="ajaxReq",produces="text/html;charset=utf-8") @ResponseBody public String ajaxReq(HttpServletRequest req,HttpServletResponse resp) throws IOException{ String username = req.getParameter("username"); String password = req.getParameter("password"); return username+" : "+password; }