Solutions for ajax to transmit Chinese garbled characters: 1. Set a unified encoding method; 2. Server-side encoding; 3. Client-side decoding; 4. Set HTTP response headers; 5. Use JSON format. Detailed introduction: 1. Set a unified encoding method to ensure that the server and client use the same encoding method. Under normal circumstances, UTF-8 is a commonly used encoding method because it can support multiple languages and character sets; 2 , Server-side encoding. On the server side, ensure that the Chinese data is encoded in the correct encoding method and then passed to the client, etc.
The problem of garbled characters when Ajax transmits Chinese is mainly caused by inconsistent encoding. In order to solve this problem, you can consider the following methods:
1. Set a unified encoding method: Make sure that the server and client use the same encoding method. Typically, UTF-8 is a commonly used encoding because it can support multiple languages and character sets. Make sure that both the server and client use UTF-8 encoding to avoid garbled characters.
2. Server-side encoding: On the server side, ensure that the Chinese data is encoded in the correct encoding method and then passed to the client. For example, you can use the getBytes("UTF-8") method in Java to convert Chinese into a UTF-8 encoded byte array, and then pass it to the client through Ajax.
3. Client decoding: On the client side, when receiving Chinese data from the server, it needs to use the correct decoding method for decoding. For example, you can use the decodeURIComponent() function in JavaScript to decode URL-encoded Chinese. The decoded Chinese string can be displayed normally on the page.
4. Set the HTTP response header: On the server side, you can specify the character encoding method by setting the HTTP response header. For example, in Java Servlet, you can use response.setContentType("application/json; charset=UTF-8") to set the character encoding of the response header to UTF-8. In this way, the data sent from the server to the client will be transmitted in UTF-8 encoding.
5. Use JSON format: JSON is a commonly used data exchange format that supports multiple languages and character sets. In Ajax, you can consider passing data in JSON format. JSON has its own encoding and decoding functions, which can automatically convert Chinese strings into UTF-8 encoded byte arrays, and can automatically decode into Chinese strings on the client. In this way, the trouble of manual coding and decoding can be reduced, and the readability and maintainability of the code can be improved.
The following is an example of using JSON format to transmit Chinese and solve the garbled problem:
Server-side code (Java):
import org.json.JSONObject; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.nio.charset.StandardCharsets; public class MyServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String chinese = "中文数据"; String json = new JSONObject().put("message", chinese).toString(); response.setContentType("application/json; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(json); } }
Client-side code (JavaScript):
$.ajax({ url: '/my-servlet', type: 'POST', dataType: 'json', success: function(response) { var message = response.message; // 中文数据已正确解码为字符串 console.log(message); // 输出:中文数据 } });
In this example, the server wraps the Chinese string in a JSON object and outputs the response in UTF-8 encoding. The client sends a request through jQuery's Ajax function and specifies dataType as json. In this way, the JSON data returned by the server will be automatically decoded into JavaScript objects, and the Chinese strings in it will also be decoded into normally displayed strings.
The above is the detailed content of What to do if ajax transmits Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!