Solve the garbled problem of asynchronous submission of JQuery forms to actions (ajaxform, ajaxsubmit)

Source: Internet
Author: User

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
 

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.