Home > Web Front-end > JS Tutorial > What should I do if the Chinese characters in the data are garbled when Ajax loads JSON data?

What should I do if the Chinese characters in the data are garbled when Ajax loads JSON data?

php中世界最好的语言
Release: 2018-03-30 17:41:14
Original
1666 people have browsed it

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"] 
}
Copy after login

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>
Copy after login

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" 
  } 
]
Copy after login

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>
Copy after login

The details are as follows:

Java code

 
   
    application/json;charset=UTF-8 
  text/html;charset=UTF-8 
  <value>text/plain;charset=UTF-8</value> 
   
Copy after login

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 use

for undefined

The 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template