Post commit data of jquery ajax garbled by chrome and ie

Source: Internet
Author: User

We found that jquery ajax is used today. when submitting the post data, we will find that the data in ff is normal. However, in chrome and IE browsers, all the previous post data is garbled, and most of the garbled characters are Encoding Problems, the following is a small Editor to analyze the solution.

When jquery is used to process html5 applications, it is normal to test in firefox all the time, and garbled characters are reported when users use pad for access,
I tried it myself and found that this problem exists in chrome and ie kernel. When the page attribute is set to UTF-8, only firefox is the header file of charset = UTF-8.
Chrome and ie are not specified, so garbled characters occur.

Solution:

The Code is as follows: Copy code

$. AjaxSetup ({
ContentType: "application/x-www-form-urlencoded; charset = UTF-8"
});
$. Post ("test. php", {name: "i5a6", time: "2 "},
Function (data ){
Process (data );
}, "Json ");

Or use:

The Code is as follows: Copy code

$. Ajax ({
Url: url,
Type: "POST ",
Data: data,
ContentType: "application/x-www-form-urlencoded; charset = UTF-8 ",
DataType: "json ",
Success: function (){
...
}
})

The first type is recommended, but it is also based on your actual situation. It is recommended that encodeURIComponent be used for character conversion.

To sum up some ajax experience in submitting garbled data

To avoid garbled characters, perform the following steps:

Solution

1. ensure the consistency of the encoding, including file encoding, database encoding, and webpage content-type encoding.
Check <meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/>

It is recommended to use UTF-8 Chinese, the use of gbk/gb2312 may be garbled

2. Use post instead of get
The get method transmits parameters through links and automatically uses urlEncode. The encoding methods of different browsers may be different. Using post can avoid this situation.

3. Send the escape code at the js front end, and then obtain the data through backend decoding.
These can be searched online

4. Set contentType globally and specify the encoding.
Because jquery ajax uses UTF-8 to encode and send data, ie does not add charset = UTF-8 when sending, leading to garbled code (IE uses iso-8859-1 encoding by default)

The Code is as follows: Copy code

$. AjaxSetup ({
ContentType: "application/x-www-form-urlencoded; charset = UTF-8"
});

Related Article

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.