When learning JQuery's asynchronous form submission, Chinese garbled characters may occur:
Currently, two solutions are available:
First, it is relatively earthy, that is, in the action, use
System. out. println (new String (this. testField1.getBytes ("ISO-8859-1"), "UTF-8"); to handle. However, if the attribute of a class is passed, it is obviously a little troublesome.
Second, it is more advanced. JQuery provides configurable parameters to solve the problem:
$ ("# JqueryForm"). submit (function (){
$ (This). ajaxSubmit (
// Add parameters to solve the Garbled text, but do not execute the callback
{ContentType: "application/x-www-form-urlencoded; charset = UTF-8 ",
Success: function (){
$ ("# TestOut" ).html ("submitted successfully, welcome back"). show ();
}
});
Return false; // block form submission
});
To be accurate, add contentType: "application/x-www-form-urlencoded; charset = UTF-8.
========================================================== ========================================================== ========================
Cause:
The AJAX submission of JQuery 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.
Check the Header and find that the Content-Type in Entity is different.
In Firefox, Content-Type specifies the UTF-8 character set. Www.2cto.com
In IE, no character set is specified,
Obviously, by default, the character encoding submitted asynchronously by AJAX should be consistent with that of the webpage itself. That is to say, 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 ).
Check JQuery's AJAX Tool Function Description and find that there is a specified content-type parameter in options.
Therefore, you must specify the encoding type during submission.
ContentType: "application/x-www-form-urlencoded; charset = UTF-8 ",
Author angus_17