JQuery Ajax Submit Chinese garbled solution _jquery

Source: Internet
Author: User
Tags character set

The phenomenon is as follows:

1 in Firefox, the processing of the page encoding for GB2312, the submission of data is not a problem, Chinese can be correctly resolved;

2 in IE8, the processing of the page encoding for GB2312, the submission of Chinese data garbled.

Whether it's $.post or $.ajax, or $.ajaxsubmit (from the form plug-in), there are no problems with the previous UTF-8-encoded site, which appears to have been caused by the coded format of the page that submitted the data. Anyway, since there is a difference between browsers, or from the HTTP package to see what the problem.

Open fiddle, respectively, using Firefox and IE to do an AJAX submission (take user login as an example), check their HTTP headers, find

1 The form data submitted by two browsers is consistent and is UTF-8 encoded, as shown in the following figure.


Analysis: jquery Ajax submission, will be submitted to the data to encode, using encodeURIComponent in JS processing data. Therefore, whether it is Firefox or IE, the submitted data are consistent, are UTF-8 encoded data.

2 Check header to find differences in Content-type in entity

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


In IE8, there is no character set designation.


Analysis: Obviously, by default, the character encoding of an AJAX asynchronous commit should be consistent with the Web page itself, that is, the server end uses GB2312 to decode the data without discovering the charset specified by the display (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, coupled with the charset display specified, resulting in the server using UTF-8 to decode the data (correct decoding).

Inference: It seems to solve this Chinese garbled problem, you must give Ajax asynchronous submission of the specified display charset!

Look at the description of the Ajax tool function in jquery and discover that there is a parameter in the options that specifies Content-type, and add my Ajax code:

Copy Code code 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!!!

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.