博客
关于我
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 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>