Talking about the processing way of the _javascript with the plus sign (+) in the js transfer parameter technique

Source: Internet
Author: User
Tags base64 alphanumeric characters

In general, the parameters in the URL should use the URL encoding rules, that is, the parameter string in addition to-_. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-bit hexadecimal number, and the space is encoded as a plus (+).

But for parameters with Chinese, this encoding makes the encoded string very long.

If you want to encode a parameter in a shorter way, you can encode the string in base64 encoding, but Base64 encoding cannot handle the Chinese in JavaScript, because the Chinese in JavaScript are saved in UTF-16 mode.

Base64 can only handle single-byte characters, so it is not possible to encode a JavaScript string with Chinese directly using Base64.

However, it is possible to convert UTF-16 encoded Chinese into UTF-8 by utf.js the Utf16to8 provided in this program and then base64 code.

This encoded string, after being passed to the server side, can be decoded directly through Base64_decode into a UTF-8 Chinese string.

But there is one more question to be aware of.

The plus sign (+) is used in the Base64 encoding, and the + is treated as a space when the URL is passed, so it is necessary to replace the plus sign in the Base64 encoded string with%2B to be passed as a URL parameter.

Otherwise, there will be an error after the server-side decoding.

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

The above is a small series for everyone to talk about in JS transfer parameters including the plus (+) way of handling all the content, I hope that we support cloud Habitat Community ~

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.