JQuery Ajax to submit Chinese garbled solution

Source: Internet
Author: User

? jquery is a very good framework for using jquery to submit data in specific situations, which is quite handy and quick. However, when processing an GB2312 encoded website Ajax submission, the Chinese data is garbled.

The phenomenon is as follows:

1) in Firefox, the processing page encoding for GB2312, submit data no problem, Chinese can correctly parse;

2) under IE8, the processing page encoding is gb2312, the submission of Chinese data garbled.

Whether it is $.post or $.ajax, or $.ajaxsubmit (from the form plug-in), in the previous UTF-8 encoded site did not have any problems, it appears that the data submitted by the encoding format of the page caused. Anyway, since there is a difference between browsers, or from the HTTP package to see what is the problem it.

Open fiddle, using Firefox and IE to do an AJAX submission (in the case of user login), check their HTTP headers, found

1) The form data submitted by two browsers are consistent and are UTF-8 encoded, as shown in.


Analysis: The AJAX submission of jquery will encode the data to be submitted, and use encodeURIComponent to process the data in JS. Therefore, whether it is Firefox or IE, the submitted data are consistent, are UTF-8 encoded data.

2) Look at the header and find that there are differences in the content-type in the entity

In Firefox, Content-type specifies that the character set is Utf-8.


In IE8, however, there is no character set specified.


Analysis: Obviously, by default, the character encoding of an AJAX asynchronous commit should be consistent with the Web page itself, that is, the server side uses gb2312 to decode the data without discovering the displayed charset designation (but the data was UTF-8 encoded before it was submitted). This is why under IE will appear garbled source, and in Firefox, the browser in the submission of AJAX data, with the charset display specified, resulting in the server side using UTF-8 to decode the data (correct decoding).

Inference: It seems to solve this problem of Chinese garbled, we must give Ajax asynchronous commit to specify the display of the charset!

Immediately review the description of the jquery Ajax tool function and find the options with a specified content-type parameter, add the following to my Ajax code:

The code is as follows:


JQuery (Form). Ajaxsubmit ({
URL: "Ajax.aspx?a=memberlogin",
Type: "Post",
DataType: "JSON",
ContentType: "application/x-www-form-urlencoded; Charset=utf-8 ",
Success:showloginresponse
});


Test, ok!!!


JQuery Ajax to submit a Chinese garbled solution

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.