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

你可能感兴趣的文章
MS Edge浏览器“STATUS_INVALID_IMAGE_HASH“兼容性问题
查看>>
ms sql server 2008 sp2更新异常
查看>>
MS UC 2013-0-Prepare Tool
查看>>
MSBuild 教程(2)
查看>>
msbuild发布web应用程序
查看>>
MSB与LSB
查看>>
MSCRM调用外部JS文件
查看>>
MSCRM调用外部JS文件
查看>>
MSEdgeDriver (Chromium) 不适用于版本 >= 79.0.313 (Canary)
查看>>
MsEdgeTTS开源项目使用教程
查看>>
msf
查看>>
MSSQL数据库查询优化(一)
查看>>
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>