JS URL to pass Chinese parameters caused by garbled problem __ garbled problem

Source: Internet
Author: User

Project encountered a garbled problem, from JS to the URL to the server, the URL has Chinese parameters, the server read the Chinese parameters are all. ", check the online JS coding related data to be resolved.


The workaround is as follows:
1, in JS to the Chinese parameters of two times transcoding

var login_name = document.getElementById ("LoginName"). Value; 
login_name = encodeURI (login_name); 
login_name = encodeURI (login_name); 

2, on the server side (business logic, that is, where to use) to decode the parameters
String loginName = paramutil.getstring (Request, "login_name"); The assigned parameter loginName
loginName = Java.net.URLDecoder.decode (LoginName, "UTF-8");/to it continue transcoding, convert to Chinese

When you use a URL for parameter passing, you often pass a parameter or URL address for a Chinese name, and a conversion error occurs while the background is being processed. When you use GB2312 on some delivery pages and use UTF8 on the receiving page, the parameters you receive may be inconsistent with what you originally received. URLs encoded using the server-side UrlEncode function are not the same as URLs encoded using the encodeURI function of client JavaScript.
Encoding methods in javascript:
1, Escape () method:
The specified string is encoded using the ISO Latin character set. All spaces, punctuation, special characters, and other non-ASCII characters will be converted into%XX-formatted character encodings (XX equals the encoded 16 digits of the character in the character set table). For example, spaces's corresponding encoding is%20. The Unescape method is the opposite. Characters that will not be encoded by this method: @ */+
2, encodeURI () method: The URI string is converted into an escape format string using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? + '
3, encodeURIComponent () method: The URI string is converted into an escape format string using the UTF-8 encoding format. This method encodes more characters than encodeURI (), such as/or characters. So if a string contains several parts of the URI, this method cannot be encoded, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )

Therefore, for Chinese strings, you only need to use escape if you do not want to convert the string encoding format into a UTF-8 format (such as when the original page and the target page charset are consistent). If your page is GB2312 or other code, and the page that accepts the parameter is UTF-8 encoded, you should use encodeURI or encodeuricomponent.

In addition, Encodeuri/encodeuricomponent was introduced after javascript1.5, and escape was in the javascript1.0 version.

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.