encodeURI to solve the Chinese problem when URL is passed

Source: Internet
Author: User

In the Ajax browser to send data, it is generally the default is UTF-8 encoding.

Use the methods provided in jquery to do the work

encodeURI
function Verify () {
Method of solving Chinese mess problem 1, the data sent by the page is encodeURI once,
The server segment is used for transfer into UTF-8;
To solve the problem of Chinese mess 2, the data sent from the page two times encodeURI,
The advantage of this is that regardless of the browser user setting the encoding on the page, the server uses the encoding to do a urlencode conversion to UTF-8.
var url = "Ajaxserver?name=" + encodeURI (encodeURI ($ ("#userName"). Val ()));
url = converturl (URL);
$.get (url,null,function (data) {
$ ("#result"). HTML (data);
});
}

Description: 1. Encodeurl function is mainly to do the URI to transcode, it is the default is to use the UTF-8 encoding.
2. UTF-8 encoded format: A Chinese character to a three-byte composition, each byte will be converted to 16 encoding, while adding the% number.

Here are the reasons to do the encodeURI two times and explain::

The specific analysis of the principle of the following, assuming that the page input Chinese is a "medium", follow the steps below to decode
1. For the first time, get the byte array into [ -28,-72-83] by Utf-8 Way, iterate over the byte-code array, convert each byte to the corresponding 16 encodeURI, and turn it into [E4,b8,ad] and finally [%e4,%b8,%ad]

2. The second encodeURI, turns the array into [%25E4,%25B8,%25AD] and then sends the processed data [%25E4,%25B8,%25AD] to the server side,
When the application server calls the GetParameter method, the GetParameter method goes to the application server to request parameters
The application server initially obtained is sent to the [%25E4,%25B8,%25AD], the application server will urldecode the data operation, UrlDecode operation and Encodeurl operation is the opposite operation, the result is [%e4,%b8,%ad] , and return this value to the GetParameter method

And then call the appropriate URL transcoding method or function in the server side can restore the data to the original page sent over the Chinese "medium".

encodeURI to solve the Chinese problem when URL is passed

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.