Gossip Ajax pass Chinese appear garbled

Source: Internet
Author: User
Tags serialization

Say two days ago, a project in order to invoke ashx through Ajax (general handler), and pass a series of parameters, this parameter contains some Chinese, in fact, has been used, did not find any problems, but the two days suddenly appear in Chinese conversion garbled, This problem is entirely due to the unfamiliar standard, as for other people on the Internet to modify the jquery file, using escape, encodeURI, encodeURIComponent method, may be feasible, but I test did not solve the problem, may be different environment will also cause this problem , I do not object here, in addition to the later, I also tested other methods, such as Requert before and after the change of the encoding type, the use of the method of serialization, thinking and testing time spent a lot, to tell the truth, the recent garden atmosphere I do not like, But see the gateway on this issue very few people in this angle to analyze, so I still send the first page.

1, the actual project code I will not write, write-point test, the effect of the same, the following is JavaScript code. Of course, jquery was applied:

1function test() {
2    var content ="{\"Content\":\"*中国人*\"}";
3    $.post("../ashx/Handler1.ashx",
4        {
5            Content: content
6        },
7        function(data) {
8            alert(data);
9        }, "json");
10}

2, the backstage serialization code, has applied the new function of. NET framework3.5 DataContractJsonSerializer (a lot of information on the net, this kind of role I don't have to say)

1  public void ProcessRequest(HttpContext context)
2        {
3            context.Response.ContentType = "text/plain";
4            string conten = context.Request["Content"];
5            try
6            {
7                DataContractJsonSerializer dcs = new DataContractJsonSerializer(typeof(News));
8                MemoryStream ms = new MemoryStream(Encoding.GetEncoding("gb2312").GetBytes(conten));
9                News news = (News)dcs.ReadObject(ms);
10                string c = news.Content;
11            }
12            catch (Exception ex)
13            {
14                throw ex;
15            }
16        }

Analysis:

3, the above code is doomed to throw a serialized string contains invalid characters of the exception.

4, but when I passed on the client: "Chinese" changed to "Chinese people" after the abnormal disappearance.

5, why why? The reason is quite simple and stupid, why is the odd number character exception and even though not? This history is quite remote, but it is due to the coding of JavaScript, or to the coding environment of the project, JavaScript Meusin uses the "Utf-8" ISO 1 encoding to convert all Chinese characters to 3 bytes, and Utf-8 and gb2312 is to convert all Chinese characters to 2 bytes, this change hands, get, situation mutation, 3 bytes into 2 bytes, garbled with it.

6, in fact, because the application in the project is: gb2312 encoding, in the Web.config change can, in fact, as long as the change requesencoding encoding can be.

<system.web>
<globalization requestEncoding="utf-8" responseEncoding="gb2312" fileEncoding="gb2312"/>
</system.web>

7. In fact, before this, I checked the jquery coding application, which does not specify what kind of coding to use, do not need to change the source file, which is the Jquery-1.3.2.js and jquery-1.3.2-vsdoc2.js settings.

The 3362 lines located in Jquery-1.3.2.js, of course, I did not completely cut out.

1ajaxSettings: {
2        url: location.href,
3        global: true,
4        type: "GET",
5        contentType: "application/x-www-form-urlencoded",
6        processData: true,
7        async: true
8}

Related Article

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.