您的位置:首页 > Web前端 > JQuery

jQuery 发送 post 请求返回字符串乱码的解决

2015-11-15 04:40 666 查看
后端代码:

@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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jquery