博客
关于我
springmvc ajax返回数据中文乱码
阅读量:108 次
发布时间:2019-02-26

本文共 1993 字,大约阅读时间需要 6 分钟。

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 ajax返回数据中文乱码

 

方法一、配置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 ONObject(map).toString();
  21.         return result;
  22.     }

 

转载地址:http://dauu.baihongyu.com/

你可能感兴趣的文章
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>
MySQL与Oracle的数据迁移注意事项,另附转换工具链接
查看>>
mysql丢失更新问题
查看>>
MySQL两千万数据优化&迁移
查看>>
MySql中 delimiter 详解
查看>>
MYSQL中 find_in_set() 函数用法详解
查看>>
MySQL中auto_increment有什么作用?(IT枫斗者)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>