The real Jquery.ajax to pass the Chinese parameter the solution method _jquery

Source: Internet
Author: User
Tags character set
I have also recently encountered the need to pass the Chinese parameter problem. Search on the Internet, copy and paste hair everywhere is the "ultimate" "solution" is nothing more than escape (str) to transcoding, and then in the server to write a method to edit again, or with the System.Text.Encoding method to change.

I've been using the prototype framework for a long time. Used under. net-gb2312 or Jsp-utf8, never encountered a problem with character encoding. The prototype and jquery code are then downloaded to open the reason for the study. The specific results are as follows


The difference is that the jquery default contenttype:application/x-www-form-urlencoded
And prototype is the contenttype:application/x-www-form-urlencoded; Charset=utf-8
This is the reason jquery is garbled, when the character set is not specified, is the use of iso-8859-1
Iso8859-1, usually called Latin-1. Latin-1 includes additional characters that are indispensable for writing all Western European languages.
jquery Ajax does not take into account the internationalization of the problem, the use of the European character set, so it caused the transfer of Chinese garbled problem.
And our UTF-8 can solve this problem.
Finally refers to the need to modify the jquery code, the explicit declaration contenttype use the Utf-8 character set, can solve the problem of GB2312 Chinese delivery.
Modify as shown:


Simply modify the jquery code, plus the Charset=utf-8, so that you don't need to change what web.config or what is encoded in the page, and you don't need to use ESCAPC (str) to decode the server. How to transfer English, how to transfer Chinese.
Test for a simple piece of code:
Test.html:

Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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>
<body>
<p><a href= "Javascript:doajax ();" >ajaxtest</a><input name= "Tbox1" id= "Tbox1" type= "text"/></p>
<div id= "Ajaxresponse" ></div>
</body>

Ajaxtest.aspx:
Copy Code code 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>

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.