Solve Chinese garbled characters in Ajax loading JSon data, ajaxjson

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.