Home > Web Front-end > JS Tutorial > Solutions to common problems in JavaScript (garbled characters, IE cache, proxy)_javascript skills

Solutions to common problems in JavaScript (garbled characters, IE cache, proxy)_javascript skills

WBOY
Release: 2016-05-16 17:11:56
Original
1331 people have browsed it

Two commonly used methods to solve AJAX Chinese garbled characters

1. EncodeURI on the client side (utf-8 does not need to be done, the default), and convert iso-8859-1 encoding to utf-8 encoding on the server side

2. EncodeURI twice on the client side and convert it once on the server side.

The reason why the second method can solve the problem:

After two conversions, perform the first decoding 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 using URLDecoder decode method, so this problem can be solved normally. It should be noted that the decoding format needs to be specified as "utf-8" in the decode method. Many Chinese platforms do not use utf-8 (I guess it is gb2312), so the default conversion of decode is not necessarily utf-8.

The reason why the client is encoded twice and decoded only once on the server is because of Tomcat. In order to make programming convenient for programmers (get and post use the same code), Tomcat will automatically decode the encoding of the post, so there is one less handwritten decoding statement on the server side. The reason why we need to perform encoding and decoding again is because the automatic decoding operation of Tomcat is not necessarily decoded according to the encoding we want, but the codes decoded for English and other characters are the same no matter what platform they are on, so it can be Tomcat automatically interprets the result of the first encoding, and then manually interprets the encodeURI code once to obtain the format you need.

Additional: Now I have observed the behavior of the browser again, and I feel that it is not the fault of Tomcat, because the Chinese displayed in the browser is not the encoded content. I am now confused about these encoding issues. I'm confused, I hope friends who know this knowledge can give me some advice!

Solving the IE cache problem
Add a timestamp and want to check it?

Solve the agency problem
Want to do this? Convert to $

Sample code:

Copy code The code is as follows:

function verify() {
//Method 1 to solve the Chinese garbled problem, the data sent from the page side is encoded as an encodeURI, and the server segment uses new String(old.getBytes("iso8859-1"), "UTF-8");
// Method 2 to solve the problem of Chinese garbled characters. The data sent from the page side is encoded twice as encodeURI. In the server segment, use URLDecoder.decode(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, fool the browser and not read the cache
function convertURL(url) {
// Get the timestamp
var timstamp = (new Date()).valueOf();
//Splice the timestamp information into the url
//url = "AJAXServer"
if (url. indexOf("?") >= 0) {
url = url "&t=" timstamp;
} else {
url = url "?t=" timstamp;
}
return url;
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template