springmvc ajax返回数据中文乱码

avatar 2017年09月25日12:06:33 5 2597 views
博主分享免费Java教学视频,B站账号:Java刘哥
springmvc ajax返回数据中文乱码 问题经常出现,这里提供两种解决办法。 这里是一个 登录的例子,ajax 验证用户名和密码
  1. $("#loginForm").submit(function () {
  2.         $.ajax({
  3.             async: true,
  4.             type: "POST",
  5.             url: '${pageContext.request.contextPath}/loginVerify',
  6.             contentType: "application/x-www-form-urlencoded; charset=utf-8",
  7.             data: $("#loginForm").serialize(),
  8.             dataType: "json",
  9.             success: function (data) {
  10.                 if(data.code==0) {
  11.                     alert(data.msg);
  12.                 } else {
  13.                     window.location.href="${pageContext.request.contextPath}/admin";
  14.                 }
  15.             },
  16.             error: function () {
  17.                 alert("数据获取失败")
  18.             }
  19.         })
  20.     })
 

方法一、配置springMVC编码过滤器

这种方法较为常见,在 web.xml 顶部 添加如下代码
  1. <!--post乱码过滤器-->
  2.   <!-- 配置springMVC编码过滤器 -->
  3.   <filter>
  4.     <filter-name>CharacterEncodingFilter</filter-name>
  5.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  6.     <!-- 设置过滤器中的属性值 -->
  7.     <init-param>
  8.       <param-name>encoding</param-name>
  9.       <param-value>UTF-8</param-value>
  10.     </init-param>
  11.     <!-- 启动过滤器 -->
  12.     <init-param>
  13.       <param-name>forceEncoding</param-name>
  14.       <param-value>true</param-value>
  15.     </init-param>
  16.   </filter>
  17.   <!-- 过滤所有请求 -->
  18.   <filter-mapping>
  19.     <filter-name>CharacterEncodingFilter</filter-name>
  20.     <url-pattern>/*</url-pattern>
  21.   </filter-mapping>
注意:最好把这段代码放在web.xml中开头的位置,因为拦截有顺序,如果放在后面的话容易拦截不到。  

方法二、@RequestMapping里面加入produces = “text/plain;charset=UTF-8”

  1. @RequestMapping(value = "/loginVerify",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
  2.     @ResponseBody
  3.     public String loginVerify(HttpServletRequest request) throws Exception {
  4.         Map<String, Object> map = new HashMap<String, Object>();
  5.         String user = request.getParameter("user");
  6.         String password = request.getParameter("password");
  7.         UserCustom userCustom = userService.getUserByNameOrEmail(user);
  8.         String message="";
  9.         if(userCustom==null) {
  10.             map.put("code",0);
  11.             map.put("msg","用户名无效!");
  12.         } else if(!userCustom.getUserPass().equals(password)) {
  13.             map.put("code",0);
  14.             map.put("msg","密码错误!");
  15.         } else {
  16.             map.put("code",1);
  17.             map.put("msg","");
  18.             request.getSession().setAttribute("userId", userCustom.getUserId());
  19.         }
  20.         String result = new JSONObject(map).toString();
  21.         return result;
  22.     }
   
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0