jquery to. ashx file post Chinese garbled solution for the problem _jquery

Source: Internet
Author: User
1. My environment: VS2005, not installed SP1 patches, cannot create Web applications, can only create Web sites; jquery version 1.5.1

Related Configuration in 2.web.config

<globalization requestencoding= "gb2312" responseencoding= "gb2312"/>

The writing of 3.jquery post data
Copy Code code 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 receive parameters in a. ashx file

String strstudentname = context. request.params["Studentname"];
Note: If there is no contenttype: "application/x-www-form-urlencoded; Charset=utf-8 ", then the context. request.params["Studentname"] is garbled.
After tracking context.Request.ContentEncoding in. ASHX, you can see that the data that jquery has posted is gb2312 encoding, which may context.request be decoded by default Utf-8 when receiving data, but jquery The post data is not used utf-8 to cause. ashx context.request.params["Studentname" is displayed as garbled.
A phenomenon that feels rather strange:
Phenomenon 1: In not adding contenttype: "application/x-www-form-urlencoded;" Charset=utf-8 ", in the case where the following statement is used in the. ashx file, the string can be displayed correctly:
Copy Code code as follows:

StreamReader STEAMRD = new StreamReader (HttpContext.Current.Request.InputStream);
String strpostdata = Steamrd. ReadToEnd ();
Strpostdata =httputility.urldecode (Strpostdata, encoding.getencoding ("Utf-8"));

Phenomenon 2: Change the related configuration in web.config to
<globalization requestencoding= "Utf-8" responseencoding= "Utf-8"/>
After that, whether or not to add ContentType: "application/x-www-form-urlencoded; Charset=utf-8 ", the background of the. ashx file received the parameters are still garbled. After modifying the web.config, the site compiles slowly and runs slowly.

Reference articles:
Http://www.jb51.net/article/26658.htm
Http://www.jb51.net/article/26659.htm

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.