Ajax Chinese parameter garbled

Source: Internet
Author: User
Tags httpcontext urlencode

The core of Ajax technology is JavaScript, and JavaScript uses UTF-8 encoding, so when the page uses GBK or other encoding, and there is no encoding conversion, there is a problem of Chinese garbled.

The following is a solution that uses the Get and post methods respectively, and the page uses GBK and UTF-8 to encode different test results and garbled characters in IE and ff.

Method of transmitting value Client-side encoding Server-side encoding Ie Ff Solution Solutions
GET UTF-8 UTF-8 Garbled when receiving parameters passed by $_get Normal Client Url=encodeuri (URL)
GET GBK GBK Normal Garbled when receiving parameters passed by $_get Client Url=encodeuri (URL) server-side $str =iconv ("UTF-8", "GBK", $str)
POST UTF-8 UTF-8 Garbled when receiving parameters passed by $_get Normal Client Url=encodeuri (URL)
POST UTF-8 UTF-8 Normal when receiving parameters passed by $_post Normal when receiving parameters passed by $_post Recommended mode of Use
POST GBK GBK Normal Garbled when receiving parameters passed by $_get Client Url=encodeuri (URL) server-side $str =iconv ("UTF-8", "GBK", $str)
POST GBK GBK Garbled when receiving parameters passed by $_post Garbled when receiving parameters passed by $_post Server-side $str =iconv ("UTF-8", "GBK", $str)

Another problem may exist in IE: This operation could not be completed because of an error c00ce56e.

The header (' Content-type:text/html;charset=utf8 ') is changed to header (' Content-type:text/html;charset=utf-8 ') when the encoding is set.

Examples encountered in development:

Through the jquery $.get () method to pass Chinese parameters, background receive, page encoding is not set, Firefox normal, ie garbled

var name = "China"; var url = "/movies/getmoviename?name=" + Name;$.get (url,{}, function () {}, "JSON");

Workaround:

[1] JS Request two times encoding: Url=encodeuri (URL); Url=encodeuri (URL); i.e. Url=encodeuri (encodeURI (URL));

var name = encodeURI (encodeURI ("China")); var url = "/movies/getmoviename?name=" + Name;$.get (url,{}, function () {}, "JSON")

[2] Decoding

(1) C # Background decoding:

public void Getmoviename (string name) {    //httputility.urldecode method    name = System.Web.HttpUtility.UrlDecode ( name);    Server.urldecode method    name = Server.urldecode (name);  }

(2) JS decoding: decodeURI (URL);

Note: JS, C # encoding is decoded as follows:

Escape does not encode characters with 69: *,+,-,.,/,@,_,0-9,a-z,a-z

encodeURI does not encode 82 characters:!,#,$,&, ', (,), *,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,a-z

encodeURIComponent does not encode 71 characters:!, ', (,), *,-,.,_,~,0-9,a-z,a-z

[1], Js:escape (name):

JS can use escape when using data.

For example: Search the history record of the Tibetan.

Unicode values other than 0-255 are encoded with the output%u**** format, and in other cases the Escape,encodeuri,encodeuricomponent encoding results are the same.

(1) JS decoding: unescape (name);

(2) C#:httputility.urlencode () Httputility.urldecode ();

[2], Js:encodeuri ():

encodeURI () can be used as a whole when making a URL jump

For example: Location.href=encodeuri ("http://cang.baidu.com/do/s?word= Baidu &ct=21");

(1) JS decoding: decodeURI ();

(2) C # decoding: decodeURIComponent ();

[3], JS: encodeuricomponent ():

You need to use encodeURIComponent when passing parameters so that the combined URLs are not truncated by special characters such as #.

For example: <script language= "JavaScript" >document.write (' <a href= ' http://passport.baidu.com/?logout&aid=7 & u= ' +encodeuricomponent ("http://cang.baidu.com/bruce42") + ' "> Exit </a& gt; '); </script>

(1) decoding using decodeuricomponent (U);

(2) c#:[httpcontext.current.] Server.urldecode (URL) [HttpContext.Current.] Server.URLEncode (URL);

Ajax Chinese parameter garbled

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.