码农公社 210是何含义?10月24日是程序员节,1024 =210、210既
210
之意。
一、问题描述
使用zTree异步刷新父级菜单时,服务器返回中文乱码。项目中使用SpringMvc已经对中文乱码预处理了,咋还会出现乱码呢?
请看俺的异步请求配置:
Java代码
async:{enable:true,url:basePath+'/sysMenu/listSysMenu',autoParam:["id=parentId"]}
SpringMvc中文字符处理:
Java代码
application/json;charset=UTF-8
text/html;charset=UTF-8
返回的结果有中文乱码:
Js代码
[
{
"menuId":"880095098165986816",
"menuName":"????",
"parentId":"880095098165986815",
"menuUrl":"http://www.210.cx/imovie-manage/sysMenu/listSysMenuUI",
"menuIcon":"",
"menuSort":1,
"isEnable":1,
"parentMenuName":"??",
"id":"880095098165986816",
"name":"????",
"pId":"880095098165986815"
},
{
"menuId":"880095098165986817",
"menuName":"???????",
"parentId":"880095098165986815",
"menuUrl":"http://www.210.cx/imovie-manage/sysMenu/treeSysMenuUI",
"menuIcon":"",
"menuSort":1,
"isEnable":1,
"parentMenuName":"??",
"id":"880095098165986817",
"name":"???????",
"pId":"880095098165986815"
}
]
二、解决方案
经过排查,发现是SpringMvc中文字符处理的supportedMediaTypes少了一种类型。
从浏览器发送的请求来看:
异步刷新使用的是post请求,但从服务器返回的时候,Content-Type为:text/plain;charset=ISO-8859-1
charset是ISO-8859-1,而不是UTF-8,而SpringMvc处理的中文乱码没有包含这种类型,所以导致中文乱码。
所以最后的解决方法是在SpringMvc中文处理加上text/plain这个类型,如下:
Java代码
text/plain;charset=UTF-8
具体如下:
Java代码
application/json;charset=UTF-8
text/html;charset=UTF-8
text/plain;charset=UTF-8