The escape Function solves the problem of garbled characters when ajax is passed in javascript. _ javascript tips-js tutorial

Source: Internet
Author: User
This article describes how to use the escape Function to solve Chinese garbled characters transmitted by ajax in Javascript. It is a very practical technique, for more information about how to use the escape Function to solve Chinese garbled characters transmitted by ajax in Javascript, see the example below. The specific method is as follows:

I. Problem description:

Originally in the Web Effect of escape () is the Chinese according to the iso-8859-1 Character Set url encoding, that through request. getparameter () can directly obtain the request parameters, but later javascript will replace escape () with the unicode Character Set encoding, so that, in the jsp tutorial and servlet, the request parameters cannot be obtained directly. I do not know the specific reason.

Ii. solution:

1. encode the Chinese characters twice. If you want to pass the parameter name and the value is "hello", the url format is .... name = escape ("hello. getparameter () can get the encoded parameter.

2. Because the obtained parameter is in the format of % 25u4f60% 25u597d, the standard urldecoder cannot be used. decode () to decode. Fortunately, there are enough coders in the world. I have found a tool class on the Internet to implement escape () and unescape () encoding and decoding in javascript.

The Code is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.