Home  >  Article  >  Web Front-end  >  Solution to JQuery.ajax passing Chinese parameters Recommended_jquery

Solution to JQuery.ajax passing Chinese parameters Recommended_jquery

WBOY
WBOYOriginal
2016-05-16 18:09:061009browse

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 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.

You only need to simply modify the JQuery code and add charset=UTF-8. In this way, there is no need to change the web.config or change the encoding on the page. You need to use escapc(str) and then decode on the server side. How it is conveyed in English is also conveyed in Chinese.

Test a simple code:

test.html:

Copy code The code is as follows:











AjaxTest






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

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