Common Problems in JavaScript (garbled code, ie cache, proxy)

Source: Internet
Author: User

 

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. The cause of the problem solved by the 2nd Methods: After two conversions, the first decoding is performed in the first getparameter method, because the obtained result is in English (the result after the first encode ), so there will be no problems; the second use of urldecoder's decode method, so this problem can be solved normally. 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! Add a timestamp to solve the IE cache problem and check? To solve the proxy problem? Convert to $ Sample Code: Function verify () {<br/> // method 1 to solve the problem of Chinese chaos. The data sent from the page is used as an encodeuri, and the server segment uses new string (old. getbytes ("iso8859-1"), "UTF-8"); <br/> // method 2 to solve the problem of Chinese Garbled text, the data sent by the page is made twice encodeuri, the server segment uses urldecoder. decode (old, "UTF-8") <br/> var url = "ajaxserver? Name = "+ encodeuri ($ (" # username "). val (); <br/> url = converturl (URL); <br/> $. get (URL, null, function (data) {<br/>$ ("# result" ).html (data); <br/> }); <br/>}< br/> // Add a timestamp to the URL address. The browser is cheated and the cache is not read. <br/> function converturl (URL) {<br/> // obtain the timestamp <br/> var timstamp = (new date ()). valueof (); <br/> // splice the timestamp information to the URL <br/> // URL = "ajaxserver" <br/> If (URL. indexof ("? ") >=0) {<br/> url = URL +" & t = "+ timstamp; <br/>}else {<br/> url = URL + "? T = "+ timstamp; <br/>}< br/> return URL; <br/>}
 

 

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.