There are many reasons for ajax Chinese garbled text. How can this garbled text problem be solved?
It's easy to use the built-in function encodeURIComponent of JS to escape Chinese strings. Usage of the encodeURIComponent function,
Scape () method:
The specified string is encoded using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % xx format (xx equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20.
Characters not encoded by this method: @ */+
EncodeURI () method:
Convert a URI string to an escape string in UTF-8 encoding format.
Characters not encoded by this method :! @ # $ & * () = :/;? +'
EncodeURIComponent () method:
Convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded.
Characters not encoded by this method :! *()'
Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is GB2312 or another code, and the page that accepts the parameter is UTF-8 code, use encodeURI or encodeURIComponent.
Example
1. The parameters in the sending path are in Chinese, and the received parameter values in the server segment are garbled.
Solution: The front-end (client) is encoded twice and the backend (server) is decoded once!
Front end:
| The code is as follows: |
Copy code |
Var url = "index. jsp? Test = I am not garbled from the front-end "; Url = encodeURI (url ); Url = encodeURI (url); // Two Encodings XMLHTTP. open ("post", url, true ); Backend: String name = arg0.getParameter ("test "); Name = java.net. URLDecoder. decode (name, "UTF-8"); // decode once System. out. println ("parameter passed by the front-end:" + name); // The output result is not garbled from the front-end.
|
2. The returned value of responseText or responseXML contains Chinese characters which are garbled.
Solution: specify the data sending format at the backend!
Backend:
| The code is as follows: |
Copy code |
Response. setContentType ("text/xml; charset = UTF-8"); // This line is good before stream output PrintWriter out = arg1.getWriter (); String info = "I am not garbled from the background "; Out. println (info ); Front end: Var backInfo = XMLHttpReq. responseText; // information returned by the background Alert (backInfo); // result: I am not garbled from the background
|
3. Summary
1) the default format of data submitted by Ajax is UTF-8, which is encoded twice using the encodeURI () method provided by javascript. use java.net. URLDecoder. decode ("", "UTF-8") method decoding once.
2) AJAX in receiving responseText or responseXML value is decoded according to the UTF-8 format, so the server to send data to the client, also need to use UTF-8 encoding, response. setContentType ("text/xml; charset = UTF-8 ").
3) If the above method still cannot solve the garbled problem, then you try to save jsp, htm, java file in UTF-8 encoding format. in short, the front and back-end data interaction uses UTF-8 encoding.
When the website uses GBK/GB2312 encoding, there are two types:
1. When Ajax sends Chinese characters and PHP (background program) receives garbled characters, use the conversion functions of GBK and UTF8 to encode and convert the accepted strings, and then store them in the relevant database, assume that the database uses GBK or GB2312 encoding. If the configured PHP runtime environment supports the ICONV function, you can also use the ICONV function for encoding conversion and then save the code to the database.
| The code is as follows: |
Copy code |
Mysql_query ("set names gb2312 ");
|
To solve the problem of Chinese garbled characters in Ajax value passing.
2. When PHP sends Chinese characters and Ajax (front-end page) for receiving garbled characters, you can also use the ICONV function to encode and convert the strings extracted from the database, and then pass the value to the Ajax front-end, that is, responseText. Or add
| The code is as follows: |
Copy code |
Header ('content-type: text/html; charset = gb2312 ');
|
You can. Note: When Ajax is used for multi-level association (for example, province-City Association), XML interaction is used, and header must be added before XML output.