1. My environment: vs2005, without SP1 patch, cannot create web applications, only websites; jquery version 1.5.1
2. Relevant configurations in web.config
3. How to write jquery’s Post data
$(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);
}
});
});
});
where StudentName is Chinese
4. How to write parameters in .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"] will be garbled.
By tracking context.Request.ContentEncoding in .ashx, it can be seen that the data posted by jquery uses gb2312 encoding. Maybe context.Request uses utf-8 for decoding by default when receiving data, but jquery uses utf-8 to decode the data in the Post data. However, utf-8 was not used, which caused the context.Request.Params["StudentName"] of .ashx to be displayed as garbled characters.
Strange phenomenon:
Phenomenon 1: Without adding contentType: "application/x-www-form-urlencoded; charset=UTF-8", use the following in the .ashx file The statement can display the string correctly:
StreamReader steamRd = new StreamReader(HttpContext.Current.Request.InputStream);
string strPostData = steamRd .ReadToEnd();
strPostData =HttpUtility.UrlDecode(strPostData, Encoding.GetEncoding("utf-8"));
Phenomenon 2: After changing the relevant configuration in web.config to
, regardless of whether it is added contentType: "application/x-www-form-urlencoded; charset=UTF-8", the parameters received by the .ashx file in the background are still garbled. After modifying web.config, the website compiles very slowly and runs very slowly.
Reference article:
http://www.jb51.net/article/26658.htmhttp://www.jb51.net/article /26659.htm