Home  >  Article  >  Web Front-end  >  The escape function solves the problem of garbled characters in ajax transmission in js_javascript skills

The escape function solves the problem of garbled characters in ajax transmission in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:32:341426browse

The example in this article describes how the escape function solves the problem of garbled Chinese characters in ajax transmission in js. It is shared with everyone for your reference. The specific method is as follows:

1. Problem description:

Originally, the escape() in the web page special effects encoded Chinese according to the iso-8859-1 character set, so that the request parameters can be obtained directly through request.getparameter(), but later javascript will escape() ) was replaced with unicode character set encoding. As a result, the request parameters cannot be obtained directly in the jsp tutorial and servlet. I don't know the specific reason.

2. Solution:

1. First, escape() encode the Chinese characters twice. If you want to pass the parameter name and the value is "Hello", the format of the URL is....name=escape(escape("Hello") ), in this way, the encoded parameters can be obtained in request.getparameter().

2. Since the obtained parameters are in the format of %u4f60%u597d, it cannot be decoded using the regular urldecoder.decode(). Fortunately, there are enough talented people in the world to find a tool class directly on the Internet. , can realize escape() and unescape()-style encoding and decoding in javascript

Copy code The code is as follows:

In the above example, we only used the escape function in Chinese, and the syntax is as follows:

Definition and usage:
The escape() function encodes a string so that it can be read on all computers.
Syntax:

escape(string) parameter description
string required. The string to be escaped or encoded.

Return value:
A copy of the encoded string. Some of these characters are replaced with hexadecimal escape sequences.

Description:
This method does not encode ASCII letters and numbers, nor does it encode the following ASCII punctuation characters: - _ . ! ~ * ' ( ) . All other characters will be replaced by escape sequences.

Tips and Notes:
Tip: You can use unescape() to decode escape() encoded strings.
Note: ecmascript v3 deprecates this method, applications should use decodeuri() and decodeuricomponent() instead

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
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