Ajax and Jquery.ajax Chinese parameter post pass garbled processing method

Source: Internet
Author: User
Tags character set

Ajax and Jquery.ajax Chinese parameter post pass garbled processing method

Today in the project, need to use Ajax, before I pass the Chinese parameters in get way, only need to set the page code in the background program for GB2312 that Chinese can be normal display, but this time due to more forms items, not suitable for Get way pass, only by post way, But found in the background program to set the encoding for GB2312 Chinese or display garbled. After a study of problems to finally be solved.

The solution to this problem is very simple, only need to use the escape () function in the JS processing parameters can be, and do not need to use unescape () decoding, this method is suitable for post and get way, specific AJAX code here I do not, here give out with Escape () Code when the function processes parameters:

var htmer = "getcode=" +escape (getcode) + "&content=" +escape (Content); Usually when we are dealing with Ajax, we will get the value of the parameters directly here, in order to make the Chinese parameter is not garbled, we only need to use the escape () function to deal with the parameters.

Question Two

When the Site page is not utf-8 code, the Chinese will become garbled Ajax submitted, the solution is as follows: Find the contenttype:application/x-www-form-urlencoded in Jquery.js, Change it into a contenttype:application/x-www-form-urlencoded; Charset=utf-8 is OK. Reason: When CharSet is not specified, jquery uses iso-8859-1,iso8859-1, often 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, and the use of European character set, so the transfer of Chinese will only appear garbled

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

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.

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/web Effects" src= "Scripts/jquery-1.3.2.min.js" ></script>
<script type= "Text/javascript" >
function Doajax () {
$.post ("ajaxtest.asp tutorial X", {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.