Chinese parameters are garbled during transmission in our web project. This has always been an annoying problem. The basic idea for solving the problem is: 1. encode the file parameters in the foreground. 2. After receiving parameters in the background, decode the Chinese parameters. Encoding of Chinese parameters: encodeU... SyntaxHighlighter. all ();
Chinese parameters are garbled during transmission in our web project. This has always been an annoying problem. My basic ideas for solving the problem are:
1. encode the file parameters in the foreground.
2. After receiving parameters in the background, decode the Chinese parameters.
Encoding of Chinese parameters: encodeURI (here is the Chinese parameter you want to pass ));
The background receives Chinese parameters for decoding: java.net. URLDecoder. decode (the Chinese parameters you get, "UTF-8 ");
The above two steps can basically solve the problem.
--------------------- Supplement
There are several URL string encoding methods in javascript: escape (), encodeURI (), and encodeURIComponent (). These encodings play different roles.
Escape () method:
The specified string is encoded using the ISO Latin character set. All () space characters, punctuation marks, special characters, and their non-ASCII characters will be converted into % xx format) character encoding (xx equals to the hexadecimal number of the character in the character set table ). For example, the space character () is encoded as % 20.
Will not be encoded by this method () character: @ */+
EncodeURI () method:
Converts a URI string into an escape string in UTF-8 encoding format.
Will not be encoded by this method () character :! @ # $ & * () = :/;? +'
EncodeURIComponent () method:
Converts a URI string into an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more () characters, such. Therefore, if the string contains the URI (), this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded.
Will not be encoded by this method () character :! *()'
Because of this, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format () (such as the original page and the target page () charset is consistent) you only need to use escape. If your () page is GB2312 or their () encoding, And the accept parameter () page is UTF-8 encoding (), use encodeURI or encodeURIComponent.
In addition, encodeURI/encodeURIComponent is introduced after javascript1.5, and escape is available in javascript1.0.
Parameter passing: Use encodeURI ("url parameter") to encode the url
Receiving parameter: decode with decodeURI ("received () value ")