Solve Chinese garbled characters in Ajax loading JSon data, ajaxjson
I. Problem Description
When I use zTree to asynchronously refresh the parent menu, the server returns Chinese garbled characters, but SpringMvc is used in the project and Chinese garbled characters have been processed. Why?
Configuration of asynchronous requests:
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" } ]
Ii. Solutions
After investigation, it is found that supportedMediaTypes for SpringMvc Chinese Character Processing is missing one type.
The browser sends the following requests:
Asynchronous refresh uses a post request, but the Content-Type returned from the server is: text/plain; charset = ISO-8859-1
Charset is ISO-8859-1, rather than UTF-8, and SpringMvc handles Chinese garbled characters that do not contain this type, so it causes Chinese garbled characters.
Therefore, 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
<property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> </list> </property>
Summary
The above section describes how to solve Chinese garbled characters in Ajax loading JSon data. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!