This is the solution to passing Chinese parameters through jquery. Ajax.

Source: Internet
Author: User

Many people may encounter a problem when using the jquery. Ajax method. When encoding is not a UTF-8, when the passed parameter contains Chinese, the server request will appear garbled. I recently encountered the problem of passing Chinese parameters. Search online, copy, paste, and publish the "ultimate" solution "is nothing more than escape (STR) for transcoding, and then write a method on the server to edit it again, or use system. text. the methods under encoding are exchanged.

I have been using the prototype framework for a long time. Used in. net-GB2312 or jsp-utf8, never encountered character encoding problems. Download the prototype and jquery code to open the study. The specific results are as follows:

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.

Modify

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:

<! 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/JavaScript" 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:

<% @ 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>

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.