Ajax Chinese garbled processing 1

Source: Internet
Author: User
There are three URL encoding methods
Escape (), Unescape () encodeuri (), decodeuri () encodeuricomponent (), decodeuricomponent () encoding plays different roles

Escape () method:

The specified string is encoded using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % XX format (XX equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20.

Characters not encoded by this method: @ */+

Encodeuri () method:

Convert a URI string to an escape string in UTF-8 encoding format.

Characters not encoded by this method :! @ # $ & * () = :/;? +'

Encodeuricomponent () method:

Convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeuri (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded.

Characters not encoded by this method :! *()'

Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is gb2312 or another code, and the page that accepts the parameter is UTF-8 code, use encodeuri or encodeuricomponent.

Java processing functions in the background:

JS: escape () function
Java is:
Java.net. urlencoder. encode ()
Java.net. urldecoder. Decode ()
However, this function is no longer recommended.
We recommend that you use
Java.net. urlencoder. encode (STR, encode );
Java.net. urldecoder. Decode (STR, encode );

This constitutes a unified encoding format. The following is a simple example:
On the server side:
String encode = java.net. urlencoder. encode ("Hello everyone", "UTF-8 ");
// Encode = ("% E5 % A4 % A7 % E5 % AE % B6 % E5 % a5 % BD"
String decode = java.net. urldecoder. Decode ("% E5 % A4 % A7 % E5 % AE % B6 % E5 % a5 % BD", "UTF-8 ");

Client decoding javascript:
Decodeuricomponent (XMLHTTP. responsetext );
Client Transfer Encoding:
Encodeuricomponent ("Hello everyone ")
Or
Encodeuri ("Hello everyone ")
This will solve the problem of Chinese garbled code.

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.