jquery Ajax post submission data chrome and IE garbled

Source: Internet
Author: User

In the use of jquery processing HTML5 application, has been under the Firefox test is normal, users use pad to visit when said there are garbled,
I tried it. Sure enough, after the discovery of chrome and IE kernel is the problem, this problem set the page properties for Utf-8 time, only Firefox is the Charset=utf-8 header file
Chrome and IE are not specified, so there is a garbled problem.

Workaround:

The code is as follows Copy Code

$.ajaxsetup ({
ContentType: "application/x-www-form-urlencoded; Charset=utf-8 "
});
$.post ("test.php", {name: "I5a6", Time: "2pm"},
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 () {
...
}
})

Recommend the use of the first, but also according to their own actual situation to see, some people recommend using encodeURIComponent to do character conversion

Summarize some experience of Ajax submitting data garbled

In order to avoid garbled, you can do the following steps

Solving method

1, maintain the unified coding, including file encoding, database coding, Web Content-type coding
Check the <meta http-equiv= "Content-type" content= text/html; Charset=utf-8″/>

Recommended Chinese are used UTF-8, use gbk/gb2312 may appear garbled

2, use post to send instead of get
The Get method passes the arguments through a link and automatically urlencode (encoding), and the individual browsers may encode differently. Use post to avoid this situation.

3, through the JS front-end escape code and then send, and then the background decoding to obtain data
These can be searched on the internet

4, in the global set ContentType, specify the encoding
Because jquery Ajax is the use of utf-8 to encode the data sent, IE did not add charset=utf-8 when sent, resulting in garbled (ie defaults to use iso-8859-1 code)

  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.