Home >Web Front-end >JS Tutorial >JQuery AJAX solution to submit Chinese garbled characters_jquery

JQuery AJAX solution to submit Chinese garbled characters_jquery

WBOY
WBOYOriginal
2016-05-16 18:24:00921browse

The phenomenon is as follows:

1) Under Firefox, the encoding of the processing page is gb2312, there is no problem with submitting data, and Chinese can be parsed correctly;

2) Under IE8, the encoding of the processing page is gb2312, and the submitted Chinese data appears garbled.

Whether it is $.post, $.ajax, or $.ajaxSubmit (from the Form plug-in), there have been no problems on the previous UTF-8 encoded website. It seems to be due to the encoding of the web page that submits the data. It's caused by the format. In any case, since there are differences between browsers, let's look at the HTTP package to see what the problem is.

Open Fiddle, use Firefox and IE to make an AJAX submission (taking user login as an example), check their HTTP headers, and find

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


JQuery AJAX solution to submit Chinese garbled characters_jquery

Analysis: JQuery's AJAX submission 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 is consistent and is UTF-8 encoded data.

2) Check the Header and find that there are differences in Content-Type in Entity

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


JQuery AJAX solution to submit Chinese garbled characters_jquery

In IE8, there is no character set specification.

JQuery AJAX solution to submit Chinese garbled characters_jquery

Analysis: Obviously, by default, the character encoding of AJAX's asynchronous submission should be consistent with the web page itself. That is to say, the server uses gb2312 to decode the data without finding the displayed charset specification (but the data has been UTF-8 encoded before submission), which is the root cause of garbled characters under IE, and under Firefox, when the browser submits AJAX data, it adds the display specification of charset, causing the server to use UTF-8 to decode the data (decoded correctly).

Inference: It seems that in order to solve this problem of Chinese garbled characters, the specified display charset must be asynchronously submitted to AJAX!

Immediately check the description of JQuery's AJAX tool function and find that there is a parameter specifying content-type in options. Add to my AJAX code:

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! ! !
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn