How to handle the plus sign (+) in the js pass parameter

Source: Internet
Author: User
Tags base64 encode alphanumeric characters

In general, the parameters in the URL should use the URL encoding rule, which is to put the parameter string in addition to-_. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+). But for parameters with Chinese, this encoding causes the encoded string to become very long. If you want to encode the parameter in a shorter way, you can encode the string in base64 encoding, but the Base64 encoding cannot handle the Chinese in JavaScript, because the Chinese in JavaScript is saved in UTF-16 manner. Base64 can only handle single-byte characters, so it is not possible to encode JavaScript strings with Chinese in base64 directly. However, it is possible to convert UTF-16 encoded Chinese into UTF-8 mode by utf.js the UTF16TO8 provided in this program, and then Base64 encode. This encoded string, after being passed to the server side, can be decoded into the UTF-8 string directly via Base64_decode. But there is one more question to keep in mind. The plus sign (+) is used in the Base64 encoding, and the + is treated as a space when the URL is passed, so you must replace the plus sign in the Base64 encoded string with%2B to be passed as a URL parameter. Otherwise, errors will occur after the server-side decoding.

So what we need to do is encodeURI (str). replace (/\+/g, '%2b ')

Turn: Technology Achievement Dream

How to handle the plus sign (+) in the js pass parameter

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.