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. Add the following to my AJAX code:
Copy codeThe 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 !!!