在用$.ajax进行异步调用时,通过response.getWriter().write("中文汉字")往success回调函数中传值时产生乱码:
解决方法:1.(在action中设置)response.setCharacterEncoding("UTF-8");
2.(在action中设置)response.setContentType("text/html;charset=UTF-8");
代码如下(Struts2):
public String getInitConfig() {
JSONObject json = service.getInitConfig();
response.setCharacterEncoding("UTF-8");
try {
response.getWriter().write(String.valueOf(json));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}