Home >Web Front-end >JS Tutorial >JQuery AJAX solution to submit Chinese garbled characters_jquery
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.
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.
In IE8, there is no character set specification.
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: