JQuery Ajax Post method in the transfer of Chinese garble code solution

  • 2020-03-30 04:11:41
  • OfStack

In this paper, an example is given to explain the solution of Post in JQuery, that is, the method of dealing with garbled ajax and jquery.ajax. Share with you for your reference. Specific analysis is as follows:

Problem a:

When doing the project today, need to use Ajax, I GET way before passing parameters in Chinese, just in the background Settings page in the program code for GB2312 or Chinese can display properly, but because the form item is more, res GET passed, only in the POST way, but found the background program set in the coding for GB2312 Chinese or gibberish. The problem was finally solved after some research.

Solutions:

The solution to this problem is very simple, just need to use the escape() function to handle the parameters in JS, and do not need to use the unescape() decoding, this method is suitable for POST and GET, the specific Ajax code here I do not give an example, here is the escape() function to handle the parameters of the code:

var htmer ="getcode="+escape(getcode)+"&Content="+escape(Content);
 

Normally when we are dealing with Ajax, we will get the value of the parameter directly here. In order to make the Chinese parameter not gargled, we just need to use the escape() function to process the parameter.

Problem two:

When a web page is not encoded in utf-8, the Chinese for the ajax submission becomes garble.

The solution is as follows:

Find the jquery. The contentType in js: application/x - WWW - form - urlencoded, change it to contentType: application/x - WWW - form - urlencoded; Charset = utf-8 will do.

The reason: When charset is not specified, jquery USES iso-8859-1, iso8859-1, commonly called latin-1. Latin-1 includes additional characters essential for writing all western European languages. Jquery's ajax does not take internationalization into account at all and USES the European character set, which is why the garbled code occurs when passing Chinese

I've been using the Prototype framework for a long time. It has been used under. Net-gb2312 or JSP tutorial -utf8, and has never encountered character encoding problems. Download the Prototype and JQuery code and open it up to see why.

Differed from the JQuery default contentType: application/x - WWW - form - urlencoded

While the Prototype is contentType: application/x - WWW - form - urlencoded; Charset = utf-8

This is why JQuery is scrambling, using iso-8859-1 when no character set is specified

Iso8859-1, commonly called latin-1. Latin-1 includes additional characters essential for writing all western European languages.

JQuery's Ajax does not take internationalization into account at all and USES the European character set, which causes the problem of sending garbled Chinese characters.

Our utf-8 solves this problem.

Finally refers to the code that needs to modify JQuery to explicitly declare that the contentType USES the utf-8 character set to solve the GB2312 Chinese delivery problem.

Simply change the JQuery code and add charset= utf-8, so you don't need to change anything web.config or change anything in the page, and you don't need to use escapc(STR) to decode on the server side. As English is delivered, so is Chinese.

Here's a simple code to test:

Test.html page code:

<!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/ Web page special 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:

<%@ 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, since the default Chinese character encoding for Ajax is utf-8, a safe bet is to ensure that the pages involved are uniformly encoded in utf-8.


Related articles: