Jquery Ajax solution for submitting Chinese garbled characters

Source: Internet
Author: User

Symptoms:

1) In Firefox, the processing page is encoded as gb2312, and data submission is correct;

2) In IE8, the processing page is encoded as gb2312, and garbled characters occur when submitting Chinese data.

Whether it is $. post or $. ajax, or $. ajaxsubmit (from the form plug-in), in the previous UTF-8 coding site has not encountered any problem, it seems that because of the encoding format of the submitted data page. In any case, since there are differences between browsers, let's look at the problem from the http package.

Open fiddle and use Firefox and IE for an Ajax commit (take user logon as an example). Check their HTTP header and find

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

Analysis: jquery's Ajax submission will encode the data to be submitted and use encodeuricomponent to process the data in Js. Therefore, both Firefox and IE submit data are consistent and are UTF-8-encoded data.

2) Check the header and find that the Content-Type in entity is different.

In Firefox, Content-Type specifies the UTF-8 character set.

In IE8, no character set is specified.

Analysis: Obviously, by default, the Ajax asynchronous submission character encoding should be consistent with the web page itself, that is, when the server side does not find the character set specified by the display, using gb2312 to decode the data (but the data has been coded by the UTF-8 before submission), That's why garbled characters occur in IE, and in Firefox, when the browser submits Ajax data, the charset display is specified, causing the server to use the UTF-8 to decode the data (correct decoding ).

Inference: It seems that to solve this Chinese Garbled text problem, you must specify the explicit charset for Ajax asynchronous submission!

Check out jquery's Ajax Tool Function Description and find that there is a specified Content-Type parameter in options.CodeAdd:

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

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.