jQuery 发送 post 请求返回字符串乱码的解决
后端代码:
@RequestMapping(params ="method=getCommentsBySentence",method= RequestMethod.POST)
public void getCommentsBySentence(Map<String,Object> map,@RequestParam String essayContextStr,HttpServletResponse response) throws Exception {
String sentencesCommentListStr = null;
try {
List<Map> sentencesCommentList = getCommentsBySentence(essayContextStr.replace("’", """).trim());
sentencesCommentListStr = JSON.toJSONString(sentencesCommentList);
} catch (Exception e) {
logger.error("",e);
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter(); //输出中文,这一句一定要放到response.setContentType("text/html;charset=utf-8"), response.setHeader("Cache-Control", "no-cache")后面,否则中文返回到页面是乱码
out.print(sentencesCommentListStr);
out.flush();
out.close();
}
前端代码:
$.post("${ctx}/WritingEssayAction.action?method=getCommentsBySentence",{
"essayContextStr":"${essayContextStr}"
},function(data){
alert(data);
});
参考资料:
Spring–SpringMVC3.1的ResponseBody返回字符串乱码问题解决-百联达-ITPUB博客
http://blog.itpub.net/28624388/viewspace-768058
jQuery AJAX 请求乱码 返回乱码 - hongweigg的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/hongweigg/article/details/6756278
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 国内外最流行App的移动端技术浅析(新闻类App)
- 下一篇: ajax post到后台中文乱码问题