springmvc 架构的网站,后端返回到前端的字符串是 UTF8 编码的,但是前端依然乱码,使用了 N 种方法不能解决,以下两种必可以解决:
1.使用 produces ,注明编码格式。但此注释只对高版本的 spring 生效。
@RequestMapping(value="/viewMilestone.htm",produces={"text/html;charset=UTF-8"})
@ResponseBody
2.相对于 1 ,这是一种通用的解决方法:
在后端使用 URLEncoder.encode(str, "UTF-8"); 方法将欲返回的内容转为 UTF8 格式的 uri 编码,前端使用 decodeURIComponent 函数解析即可。