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