The use of encodeURIComponent

Source: Internet
Author: User

Practice out of the truth, the project encountered a pit, filling up after the summary: coding does not need to decode

RSA encryption Field (after Base64 bit), after transmission through the url?filed=value, there is always a special character such as +, and then to the back end when the Base64 solution, found a lot of space.

Try using encodeURI to find or have a space, and finally use encodeURIComponent to solve the problem

Summary of the following article found that the more thorough.

Https://www.cnblogs.com/season-huang/p/3439277.html

First, preface

There are too many different articles about the 3 methods, but most of them are very much written around. This paper attempts to talk about these 3 methods from a practical point of view.

Two, escape and they are not the same class

To put it simply, escape is to encode the string (string) (and the other two URLs) to make them readable on all computers.
The effect after encoding is%XX or%uxxxx this form.
Where ASCII letters, numbers, @*/+, these characters will not be encoded, the rest will.
Most crucially, when you need to encode the URL, forget this method, which is used for strings and does not apply to URLs.
In fact, I have not used this method in practical work, so I will not talk more about it.

Third, the most commonly used encodeURI and encodeURIComponent URL encoding is common, so these two methods should be in practice to pay special attention to. They are encoded URLs, the only difference being the coded character range, where the encodeURI method won'tEncode the following characters ASCII letters, numbers, [email protected]#$&* () =:/,; + ' encodeURIComponent method won'tEncodes ASCII letters, numbers, ~!* () for the following characters so the encodeuricomponent is larger than the encodeURI encoding.  As a practical example, encodeURIComponent will encode http://To HTTP%3A%2F%2F and encodeURI not. Four, the most important, I what the occasion to use what method to distinguish the above is very clear, and then from the actual example to say. 1, if just encoded string, do not have a half-penny relationship with the URL, then use escape. 2, if you need to encode the entire URL, and then need to use this URL, then use encodeURI. Like what
encodeURI ("Http://www.cnblogs.com/season-huang/some other Thing");
After encoding, it becomes
"Http://www.cnblogs.com/season-huang/some%20other%20thing";

Where the space is encoded as%20. But if you use encodeURIComponent, then the result becomes

"Http%3a%2f%2fwww.cnblogs.com%2fseason-huang%2fsome%20other%20thing"

See the difference, even the "/" is encoded, the entire URL has been unable to use.

  

3, when you need to encode the parameters in the URL, then encodeURIComponent is the best way.

var param = "http://www.cnblogs.com/season-huang/"; param for parameter param = encodeuricomponent (param), var url = "http://www.cnblogs.com?next=" + param;console.log (URL)//"http ://www.cnblogs.com?next=http%3a%2f%2fwww.cnblogs.com%2fseason-huang%2f "
See the put, parameters in the "/" can be encoded, if the use of encodeURI must be a problem, because the next/is required to encode.

The use of encodeURIComponent

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.