Two common methods to solve AJAX Chinese garbled characters
1. encodeURI on the client side (UTF-8 can not be done, the default), in the server side to convert the iso-8859-1 encoding to UTF-8 encoding
2. encodeURI is performed twice on the client and converted once on the server.
Cause of the 2nd solutions:
After two conversions, perform the first decoding In the first getparameter method. Because the obtained result is English (the result after the first encode), no problem occurs; this problem can be solved normally because URLDecoder's decode method is used for the second time. You must specify the decoding format as "UTF-8" in the decode method ". Many Chinese platforms do not use UTF-8 (I guess it should be gb2312), so the default conversion of decode is not necessarily UTF-8.
I guess Tomcat is the reason for encoding twice on the client and decoding only once on the server. Tomcat automatically decodes the post encoding to make programming easier for programmers (get and post use the same Code). Therefore, a handwritten decoding statement is missing on the server. The reason why we need to perform another encoding and decoding is that the Tomcat automatic decoding operation does not necessarily follow the encoding we want, however, English characters and other characters are the same no matter on which platform the code is obtained. Therefore, Tomcat can automatically parse the result after the first encoding, and then manually interpret the code of encodeURI, you can obtain the desired format.
Supplement: Now I have observed the behavior of the browser. I think it is not because of Tomcat, because the browser displays Chinese characters rather than the encoded content, I am confused about these coding problems. I hope you will be enlightened by anyone who knows this kind of knowledge!
Solve IE cache Problems
Add a timestamp and check it?
Resolve proxy issues
To? Convert to $
Sample Code:
Copy codeThe Code is as follows:
Function verify (){
// Method 1 to solve the problem of Chinese Garbled text, the page side sends the data as an encodeURI, the server segment uses new String (old. getBytes ("iso8859-1"), "UTF-8 ");
// Method 2 to solve the problem of Chinese Garbled text, the page side sends data for two encodeURI, the server segment uses URLDecoder. decode (old, "UTF-8 ")
Var url = "AJAXServer? Name = "+ encodeURI ($ (" # userName "). val ()));
Url = convertURL (url );
$. Get (url, null, function (data ){
$ ("# Result" pai.html (data );
});
}
// Add a timestamp to the url address. The browser is cheated and the cache is not read.
Function convertURL (url ){
// Obtain the timestamp
Var timstamp = (new Date (). valueOf ();
// Splice the timestamp information to the url
// Url = "AJAXServer"
If (url. indexOf ("? ")> = 0 ){
Url = url + "& t =" + timstamp;
} Else {
Url = url + "? T = "+ timstamp;
}
Return url;
}