How does jquery post garbled Chinese characters to the. ashx file?

Source: Internet
Author: User

1. My environment: vs2005. No SP1 Patch is installed. You cannot create a Web application, but you can only create a website. jquery version 1.5.1

2. Related configuration in web. config

<Globalization requestEncoding = "gb2312" responseEncoding = "gb2312"/>

3. jquery Post Data Writing Method
Copy codeThe Code is as follows:
$ (Document). ready (function (){
$ ("# BtnSend"). click (function (){
$. Ajax ({
Type: "POST ",
Url: "PrecisionAHandle. ashx ",
ContentType: "application/x-www-form-urlencoded; charset = UTF-8 ",
Data: {"StudentId": $ ("# LblStudentId "). attr ("innerText"), "StudentName": $ ("# LblStudentName "). attr ("innerText"), "StudentAge": $ ("# txtStudentAge "). attr ("value ")},
Success: function (html ){
$ ("# TabContainer" ).html (html );
}
});
});
});

StudentName is a Chinese character.

4. The method for receiving parameters in the. ashx File

String strStudentName = context. Request. Params ["StudentName"];
NOTE: If there is no contentType: "application/x-www-form-urlencoded; charset = UTF-8", context. Request. Params ["StudentName"] is garbled.
After. trace context in ashx. request. contentEncoding: we can see that the data post by jquery adopts gb2312 encoding, maybe context. the Request uses UTF-8 for decoding by default when receiving data, but jquery does not use UTF-8 for Post data. the context of ashx. request. params ["StudentName"] is garbled.
Strange phenomenon:
Symptom 1: without adding contentType: "application/x-www-form-urlencoded; charset = UTF-8", in. the following statement is used in the ashx file to correctly display the string:
Copy codeThe Code is as follows:
StreamReader steamRd = new StreamReader (HttpContext. Current. Request. InputStream );
String strPostData = steamRd. ReadToEnd ();
StrPostData = HttpUtility. UrlDecode (strPostData, Encoding. GetEncoding ("UTF-8 "));

Symptom 2: change the configuration in web. config
<Globalization requestEncoding = "UTF-8" responseEncoding = "UTF-8"/>
After that, no matter whether or not contentType: "application/x-www-form-urlencoded; charset = UTF-8" is added, the parameters received by the. ashx file in the background are still garbled. After modifying web. config, the website compilation is slow and runs slowly.

References:
Http://www.jb51.net/article/26658.htm
Http://www.jb51.net/article/26659.htm

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.