Perfect solution for garbled Chinese Characters in Javascript and Ajax

Source: Internet
Author: User

Today, I encountered a Chinese Ajax garbled problem in one day. There are two types of Ajax garbled issues:

1. Chinese characters output by JavaScript are garbled,

For example: alert ("Chinese Garbled text test ");

The solution is relatively simple, that is, to set the values of all charset and pageEncoding in jsp to the same, generally UTF-8.

2. The second problem is that the data obtained by Ajax from the server is garbled. (I searched for n hours and tried n to find the answer)

Now I will share with you all the more effective methods I have collected: (the development environment I use is Eclipse, and I believe there are not many differences between other languages and development environments .)

For example

Var message = xmlHttp. responseText;

Alert ("message:" + message );

The output of this message is garbled.

Solution:

1. Modify the encoding. Remember to back up the code. After the code is changed, Chinese characters will become garbled.

Right-click Properties on the js file and modify Text file encode to UTF-8 (Here it should be the same as encoding in jsp)

Similarly, set the Default encoding of JavaScript source file and JSP to UTF-8 (Here it should be the same as the encoding in jsp, so that the future project will be UTF-8 encoding, we recommend using this)

2. note response. setContentType ("text/html; charset = UTF-8"); and PrintWriter out = response. location relationship of getWriter (); remember to set PrintWriter out = response. getWriter (); Put in response. setContentType ("text/html; charset = UTF-8");. Otherwise, the configured encoding will be invalid. This is why I have been confused for a long time!

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.