Ajax URL parameter Chinese garbled solution

Source: Internet
Author: User
Tags urlencode

Recently, when learning to write the portal, through the URL parameters of the AJAX string to the server encountered the problem of Chinese garbled, if the need to pass the data is not passed directly through the URL, such as using the Jquery.post parameter way to pass the variable, there will be no garbled problem, online search to find the following solution:

1.

The Chinese parameter values in the URL of JS are used two times encodeuri (), i.e.

encodeURI (encodeURI ("Chinese parameter value for URL")),

2.

Use System.Web.HttpUtility.UrlDecode (context) in C #. request["param"], System.Text.Encoding.GetEncoding ("UTF-8"));//Convert the encoding in the URL to a simplified Chinese character

Through the above method codec can solve the problem of Chinese garbled, the specific principle is as follows:

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

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;
//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 principle of analysis is as follows, 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".

Supplemental--c# URL encoding conversion URL transcoding urldecode urlencode:

Using System.Web; Reference system.web.

TextBox2.Text = System.Web.HttpUtility.UrlDecode (TextBox1.Text, System.Text.Encoding.GetEncoding ("GB2312"));// Convert the encoding in the URL to a simplified Chinese character

TextBox2.Text = System.Web.HttpUtility.UrlEncode (TextBox1.Text, System.Text.Encoding.GetEncoding ("GB2312"));// Convert a simplified Chinese character to a URL encoding

TextBox2.Text = System.Web.HttpUtility.UrlDecode (TextBox1.Text, System.Text.Encoding.GetEncoding ("BIG5"));// Converting encodings in URLs to traditional Chinese characters

TextBox2.Text = System.Web.HttpUtility.UrlEncode (TextBox1.Text, System.Text.Encoding.GetEncoding ("BIG5"));// Convert a traditional Chinese character to a URL

Reference:

Http://www.cnblogs.com/myjavawork/articles/1784748.html

Http://blog.clump.cc/cle/2013/120427269.html

Ajax URL parameter Chinese garbled solution

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.