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
Jquery code
$ (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:
View code
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:
1) http://dev.firnow.com/course/1_web/javascript/jsjs/20090303/157078.html
2) http://xyztony1985.blog.163.com/blog/static/361178201081754921150/