标签:springmvc ajax 乱码 responsebody 返回值
有人跟踪@ResponseBody
我遇到这个问题的时候,查阅了一下资料,采用了一个比较简单的方法来解决这个问题,就是需要服务器返回中文的时候不使用这个注解,而是直接用HttpServletResponse的对象来完成传输,在服务器端可以通过response.setContentType("text/plain;charset=UTF-8");来设定编码类型,这样就不会出现中文乱码了。
服务器端核心代码如下:
@RequestMapping(value = "test", method = RequestMethod.POST)
public void test(HttpServletRequest request,
HttpServletResponse response) {
String result = null;
//取得客户端传来的值
String userName = request.getParameter("userName");
//向客户端返回一句话
result = "您好!";
PrintWriter out = null;
response.setContentType("text/plain;charset=UTF-8");
try {
out = response.getWriter();
out.write(result.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
}
}
response.setContentType("text/html; charset=utf-8"); html
response.setContentType("text/plain;
charset=utf-8"); 文本
response.setContentType("application/json;
charset=utf-8"); 数据
response.setContentType("application/xml;
charset=utf-8");
(原文地址:http://blog.csdn.net/zhshulin)
SpringMVC中使用@ResponseBody注解返回值,Ajax取得中文乱码解决方法,布布扣,bubuko.com
SpringMVC中使用@ResponseBody注解返回值,Ajax取得中文乱码解决方法
标签:springmvc ajax 乱码 responsebody 返回值
原文地址:http://blog.csdn.net/zhshulin/article/details/37653257