Two common methods to solve Chinese garbled code in AJAX
1. Perform encodeURI on the client (UTF-8 can also be done without utf-8, default), and convert iso-8859-1 encoding to utf-8 encoding on the server side
2. Perform encodeURI twice on the client and convert once on the server side.
The second method can solve the problem:
After two conversions, the first decoding is performed in the first getparameter method. Because the solution is in English (the result after the first encode), there will be no problem; the second time I use the URLDecoder decode method, so this problem can be solved normally. It should be noted that in the decode method, you need to specify the decoding format as "utf-8". Many Chinese platforms do not use utf-8 (I guess it should be gb2312), so the default conversion of decode does not necessarily mean utf-8.
I guess the reason why I encode twice on the client and decode only once on the server is Tomcat. Tomcat will automatically decode the post encoding in order to facilitate programming by programmers (with the same code as the post), so it will automatically decode the post encoding, so there is no handwritten decoding statement on the server side. The reason why we have to perform another encoding and decoding is that the automatic decoding operation of Tomcat does not necessarily mean the encoding we want, but the code decoding in English and other characters is the same on which platform. Therefore, Tomcat can automatically interpret the result after the first encoding, and then manually interpret the code of encodeURI once, and you can get the format you need.
Added: I have observed the behavior of the browser again and felt that it was not Tomcat, because the browser is displayed in Chinese, not the encoded things. I am now confused about these encoding issues. I hope that friends who know this knowledge will give me some advice!
Solve IE caching problems
Add a timestamp and check it?
Solve proxy issues
Want to? Convert to $
Sample code:
function verify() {
//Method to solve the problem of Chinese confusion 1. Use the data sent on the page to encodeURI once, and use new String(("iso8859-1"),"UTF-8");
//Method for solving the problem of Chinese mess 2. Use the data sent on the page twice to encodeURI, and use the server segment (old, "UTF-8")
var url = "AJAXServer?name=" + encodeURI(encodeURI($("#userName").val()));
url = convertURL(url);
$.get(url,null,function(data){
$("#result").html(data);
});
}
//Add a timestamp to the URL address, tricked the browser and did not read the cache
function convertURL(url) {
//Get the timestamp
var timstamp = (new Date()).valueOf();
//Seave the timestamp information on the url
//url = "AJAXServer"
if (("?") >= 0) {
url = url + "&t=" + timstamp;
} else {
url = url + "?t=" + timstamp;
}
return url;
}