Home >Web Front-end >JS Tutorial >jquery solution to the problem of Chinese garbled characters posted to .ashx files_jquery

jquery solution to the problem of Chinese garbled characters posted to .ashx files_jquery

WBOY
WBOYOriginal
2016-05-16 18:09:051352browse

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

Copy code The 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);
}
});
});
});

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:
Copy the code The code is as follows:

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.htm
http://www.jb51.net/article /26659.htm
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn