Home > Web Front-end > JS Tutorial > body text

How to solve the Chinese garbled code submitted by JQuery ajaxSubmit

亚连
Release: 2018-05-25 09:47:45
Original
2648 people have browsed it

This article will share with you the solution to the problem of jquery ajax submit submitting Chinese garbled characters. Friends who are interested can follow me to learn

Most people use

jQuery(form).ajaxSubmit({ 
url: "ajaxsub.aspx?abc=test",
type: "post", 
dataType: "json", 
success: data
});
Copy after login

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.

Check the Header and find that there is a difference in the Content-Type in the Entity

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

There is no character set specification in IE.

Obviously, by default, the character encoding of AJAX asynchronous submission should be consistent with the web page itself. In other words, there is no character set on the server side. It is found that when the displayed charset is specified, gb2312 is used to decode the data (but the data has been UTF-8 encoded before submission). This is the root cause of garbled characters under IE, while under Firefox, the browser is submitting When AJAX data is provided, the display specification of charset is added, causing the server to use UTF-8 to decode the data (correct decoding).

Check the description of JQuery's AJAX tool function and found that there is a parameter specifying content-type in options

So you must specify the encoding type when submitting

contentType: "application/x-www-form-urlencoded; charset=utf-8",
Copy after login

is as follows

jQuery(form).ajaxSubmit({ 
url: "ajaxsub.aspx?abc=test", 
type: "post", 
dataType: "json", 
contentType: "application/x-www-form-urlencoded; charset=utf-8", 
success: data
});
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to solve the problem of Chinese garbled characters when JQuery ajax returns json

How to use ajax in Django framework post method

Detailed explanation of ajax jtemplate to implement dynamic paging

##

The above is the detailed content of How to solve the Chinese garbled code submitted by JQuery ajaxSubmit. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!