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!