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

WBOY
Release: 2016-05-16 18:06:11
Original
883 people have browsed it

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:










AjaxTest< ;input name="tbox1" id="tbox1" type="text" />





AjaxTest.aspx:
Copy code The code is as follows :

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template