json字符串返回到js中乱码

时间:2023-03-09 03:14:24
json字符串返回到js中乱码

  Ajax 的post请求值返回到js中时出现中文乱码的情况,但是在action中写入时并未乱码,解决办法在action中写入前,加上这两行:

request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

********************************************************************

try {
//保证返回值中文的时候不乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(json);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

**************************************************************************************************

  如若转载请注明出处,谢谢。By奋斗的小蘑菇