Home > Article > Web Front-end > Javascript and Ajax Chinese garbled vomiting blood version solution_javascript skills
Today I have been dealing with the Ajax Chinese garbled code problem. There are two types of Ajax garbled code problems:
1. Chinese garbled code output by JavaScript,
For example: alert("Chinese garbled code test");
The solution is relatively simple, which is to set all charset and pageEncoding values in jsp to the same, usually 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 methods to find the answer)
Now I will share with you the more effective methods I have collected: (The development environment I use is Eclipse, I believe other languages and development environments are similar.)
For example
var message = xmlHttp.responseText;
alert("message: " message);
The message output is garbled
Solution:
1. Modify the encoding. Remember to back up the code. After changing the encoding, the Chinese will become garbled.
Right-click Properties on the js file, Modify Text file encode to UTF-8 ( This should be the same as the encoding in jsp)
Similarly set the Default encoding of JavaScript source file and JSP to UTF-8 (This should be the same as the encoding in jsp, In this way, future projects will be It is utf-8 encoding, it is recommended to use this)
2. Pay attention to the positional relationship between response.setContentType("text/html;charset=utf-8"); and PrintWriter out = response.getWriter();. Remember to replace PrintWriter out = response.getWriter(); is placed after response.setContentType("text/html;charset=utf-8");, otherwise the set encoding will be invalid, which is why I couldn't figure it out after trying it for a long time! ! !
Problem solved.