Chinese garbled characters passed by JQueryAjaxPost

Source: Internet
Author: User
JQueryAjaxPost transmits Chinese garbled characters ajax and jquery. the ajax Chinese parameter post transmission garbled processing method needs to use Ajax for project implementation today. When I used GET to pass Chinese parameters, you only need to set the Page code to GB2312 in the background program, that is, the Chinese can be displayed normally... syntaxHighlighter.

JQuery Ajax Post transmits garbled Chinese characters ajax and jquery. ajax Chinese parameters post transmission garbled Processing Method

Ajax is required for project development today. When I used GET to pass Chinese parameters, I only needed to set the page encoding in the background program to GB2312 to display Chinese properly, however, due to the large number of form items, the GET method is not suitable for transmission. The POST method is only used, but the garbled characters are still displayed when the encoding is set to GB2312 in the background program. After some research, the problem was finally solved.

The solution to this problem is very simple. You only need to use the escape () function to process parameters in JS, and you do not need to use unescape () to decode the parameters, this method is suitable for POST and GET methods. The specific Ajax code is not provided here. The code for processing parameters using the escape () function is provided here:

Var htmer = "getcode =" + escape (getcode) + "& Content =" + escape (Content ); // We usually obtain the parameter value here when processing Ajax. To make Chinese parameters not garbled, we only need to use the escape () function to process the parameters.

Question 2

When the website page is not UTF-8 encoded, the Chinese text submitted by ajax will become garbled. The solution is as follows: Find jquery. in js, change contentType: application/x-www-form-urlencoded to contentType: application/x-www-form-urlencoded; charset = UTF-8. Cause: When charset is not specified, jquery uses a ISO-8859-1, A ISO8859-1, usually called Latin-1. Latin-1 includes additional characters that are indispensable for writing all Western European languages. Jquery's ajax does not take into account the internationalization problem at all, but uses the European character set, so garbled characters only occur when passing Chinese characters.

I have been using the Prototype framework for a long time. In. net-GB2312 or jsp tutorial-utf8 have been used, never encountered character encoding problems. Download the Prototype and JQuery code to open the study.

The difference is that JQuery's default contentType: application/x-www-form-urlencoded

Prototype is contentType: application/x-www-form-urlencoded; charset = UTF-8

This is the reason why JQuery is garbled, when the character set is not specified, is to use ISO-8859-1

ISO8859-1, usually called Latin-1. Latin-1 includes additional characters that are indispensable for writing all Western European languages.

JQuery's Ajax does not take into account the internationalization problem at all. It uses the European character set, so it causes the problem of garbled characters when passing Chinese characters.

Our UTF-8 can solve this problem.

Ultimately, JQuery code needs to be modified to explicitly declare that contentType uses the UTF-8 character set to solve the problem of GB2312 Chinese transmission.

Just need simple JQuery code to modify, plus charset = UTF-8 can be, so do not need to change what web. you do not need to use escapc (str) to decode config or something on the page. How to deliver English and Chinese.

 

Test the simple code:

Test.html:






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.