In JQuery Ajax, THE Post method is used to transmit garbled characters in Chinese. jqueryajax

Source: Internet
Author: User

In JQuery Ajax, THE Post method is used to transmit garbled characters in Chinese. jqueryajax

This article describes the solution for passing Post in JQuery in Chinese, that is, garbled ajax and jquery. ajax Chinese parameters. Share it with you for your reference. The specific analysis is as follows:

Question 1:

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.

Solution:

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:

Copy codeThe Code is as follows: 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 the contentType: application/x-www-form-urlencoded in jquery. js and change it 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.

Here is a simple code test:

Test.html Page code:

Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> </title>
<Script type = "text/Webpage effects" src = "scripts/jquery-1.3.2.min.js"> </script>
<Script type = "text/javascript">
Function DoAjax (){
$. Post ("AjaxTest. aspx", {txt: $ ("# tbox1"). val ()},
Function (data ){
$ ("# AjaxResponse"). text (data );
}
);
}
</Script>
</Head>

<Body>
<P> <a href = "javascript: DoAjax (); "> AjaxTest </a> <input name =" tbox1 "id =" tbox1 "type =" text "/> </p>
<Div id = "AjaxResponse"> </div>
</Body>
</Html>

AjaxTest. aspx processing page:

Copy codeThe Code is as follows: <% @ Page Language = "C #" ContentType = "text/html" ResponseEncoding = "gb2312" %>
<Script runat = "server">
String parms;

Void Page_Load (object sender, EventArgs e)
{
Parms = Request ["txt"];
If (String. IsNullOrEmpty (parms )){
Response. Write ("Is Null ");
} Else {
Response. Write (parms );
}
}
</Script>

In addition, because Ajax uses UTF-8 by default, it is safer to ensure that the unified page encoding format is UTF-8.

I hope this article will help you with jQuery-based Ajax programming.


The jquery ajax post Server Returns garbled characters when xml is Chinese characters. How can this problem be solved?

Encode Chinese characters on the server side as ASCII before returning them, for example, & #32; (decimal) or & # x20; (hexadecimal). This usually saves the decoding process, because the browser can directly decode this encoding.
The cause of the problem is that Ajax works in UTF-8 mode, which may not match the page encoding.

Baidu has not been able to find the answer for a long time. For the Chinese garbled characters returned by the jquery ajax method

Add charset restrictions to the ajax server processing page
For example, charset = gb2312 or UTF-8 must be consistent with the webpage

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.