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 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:
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/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:
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>