博客
关于我
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/

你可能感兴趣的文章
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>
myeclipse配置springmvc教程
查看>>
MyEclipse配置SVN
查看>>
MTCNN 人脸检测
查看>>
MyEcplise中SpringBoot怎样定制启动banner?
查看>>
MyPython
查看>>
MTD技术介绍
查看>>
MySQL
查看>>
MySQL
查看>>
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mysql 1045解决方法
查看>>