This time I will bring you how to deal with Chinese garbled characters in the data when Ajax loads JSon data, and how to solve the problem of Chinese garbled characters in the data when Ajax loads JSon data. take a look.
1. Problem descriptionWhen using zTree’s asynchronous refresh of the parent menu, the server returns Chinese garbled characters, but SpringMvc is used in the project. The Chinese garbled characters have been processed, why do they still appear?
Here is the configuration of asynchronous request:
Java code
async: { enable: true, url: basePath + '/sysMenu/listSysMenu', autoParam: ["id=parentId"] }
SpringMvc Chinese character processing:
Java code
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
The returned results contain Chinese garbled characters:
Js code
[ { "menuId": "880095098165986816", "menuName": "????", "parentId": "880095098165986815", "menuUrl": "http://localhost:8080/imovie-manage/sysMenu/listSysMenuUI", "menuIcon": "", "menuSort": 1, "isEnable": 1, "parentMenuName": "??", "id": "880095098165986816", "name": "????", "pId": "880095098165986815" }, { "menuId": "880095098165986817", "menuName": "???????", "parentId": "880095098165986815", "menuUrl": "http://localhost:8080/imovie-manage/sysMenu/treeSysMenuUI", "menuIcon": "", "menuSort": 1, "isEnable": 1, "parentMenuName": "??", "id": "880095098165986817", "name": "???????", "pId": "880095098165986815" } ]
2. SolutionAfter investigation, it was found that it was SpringMvc There is one missing type in supportedMediaTypes for Chinese character processing.
Judging from the request sent by the browser:
Asynchronous refresh uses
post request, but when returned from the server, the Content-Type is: text/plain ;charset=ISO-8859-1charset is ISO-8859-1, not UTF-8, and the Chinese garbled characters processed by SpringMvc do not contain this type, so Chinese garbled characters are caused.
So the final solution is to add the text/plain type to SpringMvc Chinese processing, as follows:
Java code
<value>text/plain;charset=UTF-8</value>
The details are as follows:
Java code
application/json;charset=UTF-8 text/html;charset=UTF-8 <value>text/plain;charset=UTF-8</value>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Ajax+Servlet to implement non-refresh drop-down linkage (with code)ajax to obtain json data How to useThe above is the detailed content of What should I do if the Chinese characters in the data are garbled when Ajax loads JSON data?. For more information, please follow other related articles on the PHP Chinese website!