Home Web Front-end JS Tutorial The real solution to JQuery.ajax passing Chinese parameters_jquery

The real solution to JQuery.ajax passing Chinese parameters_jquery

May 16, 2016 pm 06:06 PM
ajax Chinese parameters

I have recently encountered the problem of needing to pass Chinese parameters. After searching on the Internet, the "ultimate" "solution" that was copied and pasted everywhere is nothing more than escape(str) to transcode, and then write a method on the server to edit again, or use System.Text.Encoding Change the following methods back and forth.

I have been using the Prototype framework a long time ago. I have used it under .net-GB2312 or jsp-utf8, and I have never encountered any character encoding problems. So I downloaded both the Prototype and JQuery codes and opened them to study the reasons. The specific results are as follows

JQuery与Prototype创建Ajax代码比较
The difference is that JQuery’s default contentType: application/x-www-form-urlencoded
and 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, ISO-8859-1 is used.
ISO8859-1, usually called Latin-1. Latin-1 includes additional characters indispensable for writing all Western European languages.
JQuery’s Ajax did not consider the issue of internationalization at all and used the European character set, which caused the problem of garbled characters when transmitting Chinese.
Our UTF-8 can solve this problem.
Ultimately, it means that you need to modify the JQuery code and explicitly declare that the contentType uses the utf-8 character set, which can solve the problem of GB2312 Chinese transmission.
Modify as shown:

修改JQuery代码
You only need to simply modify the JQuery code and add charset=UTF-8. This way, there is no need to change the web.config or change the encoding on the page. , and there is no need to use escapc(str) to decode on the server side. How it is conveyed in English is also conveyed in Chinese.
Test a simple code:
test.html:

Copy the code The 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>&lt ;input name="tbox1" id="tbox1" type="text" /></p>
<div id="AjaxResponse"></div>
</body> ;
</html>

AjaxTest.aspx:
Copy code The 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>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

How to solve the 403 error encountered by jQuery AJAX request

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

How to solve jQuery AJAX request 403 error

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

How to get variables from PHP method using Ajax?

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQuery AJAX error 403?

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

PHP vs. Ajax: Solutions for creating dynamically loaded content

Understanding Ajax Frameworks: Explore Five Common Frameworks Understanding Ajax Frameworks: Explore Five Common Frameworks Jan 26, 2024 am 09:28 AM

Understanding Ajax Frameworks: Explore Five Common Frameworks

Asynchronous data exchange using Ajax functions Asynchronous data exchange using Ajax functions Jan 26, 2024 am 09:41 AM

Asynchronous data exchange using Ajax functions

What are the ajax versions? What are the ajax versions? Nov 22, 2023 pm 02:00 PM

What are the ajax versions?

See all articles