入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2015-11-15 投稿人: 浏览次数:3714

后端代码:

@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

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。