FAQ solutions for JavaScript (garbled, IE caching, proxies) _javascript tips

Source: Internet
Author: User
Tags tomcat

Two ways to solve the common Chinese characters of Ajax

1. encodeURI on the client (Utf-8 also can not do, default), on the server side of the Iso-8859-1 code into UTF-8 encoding

2. Two encodeURI on the client and one conversion on the server side.

The 2nd approach solves the problem:

After two conversions, the first decoding is performed in the first GetParameter method, because the solution is English (the result after the first encode), so there is no problem, the second use of the Urldecoder decode method, so can solve this problem normally. It should be noted that in the Decode method the decoding format needs to be specified as "Utf-8". Many Chinese platforms use not utf-8 (I guess it should be gb2312), so the decode default conversion is not necessarily utf-8.

The reason for this is Tomcat, which is two times encoded on the client and only one decoding at the server. Tomcat decodes the post encoding automatically to allow programmers to program easily (get and post use the same code), so one less handwritten decoding statement on the server side. The reason why we have to do another encoding and decoding, because the operation of Tomcat automatic decoding is not necessarily according to the code we want to solve, but the English characters no matter what platform to solve the code is the same, so you can let Tomcat automatically interpret the first coding results, Then manually interpret encodeURI code once, you can get the format you want.

Add: Now look at the browser's behavior, that is not tomcat's sake, because in the browser is displayed in Chinese, rather than after the encoding of things, for these coding problems are now confused, I hope to know this knowledge of the friends are not hesitate to enlighten!

Solve the problem of IE caching
Add a time stamp and check it?

Resolve Agent Issues
Going to be? Convert to $

Sample code:

Copy Code code as follows:

function Verify () {
method to solve the problem of Chinese tangle 1, the data from the page end is encodeURI, the server segment uses the new String (Old.getbytes ("iso8859-1"), "UTF-8");
To solve the problem of Chinese mess 2, the page end of the data issued two times encodeURI, server segment using 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 timestamp to URL address, cheat browser, do not read cache
function Converturl (URL) {
Get time stamp
var timstamp = (new Date ()). valueof ();
To stitch timestamp information to a URL
url = "Ajaxserver"
if (Url.indexof ("?") >= 0) {
url = url + "&t=" + timstamp;
} else {
url = url + "? t=" + Timstamp;
}
return URL;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.