url parameter special symbol problem

Special characters in url usually cause unnecessary problems. Currently, the url parameters used online include {}[], and the application directly returns a 400 error ( request error. The server cannot understand the request due to the wrong syntax format. Without modification, the client program cannot repeat this request). At that time, the first thing that came to mind was to escape the encodeURI. After the url is escaped, it can be used normally.

Special characters for URLs

When several specific sets of characters appear in the URL, you must pay special attention:

First, there are special characters in URLs, which are reserved characters :

; / ? : @ & = + $ , {10}

This means that when these characters are usually used in URLs, they have special meanings (such as ":" to separate each part),
if a part of a URL (such as part of a query parameter) may contain these characters one, it should
be escaped before putting it in the URL .

 

The second group of character sets to be aware of is the non- reserved character set . As follows:

- _ . ! ~ * ' ( ) {9}
These characters can be used anywhere in the URL (in some places, they are not allowed).
When using them as part of the URL, you don't need to encode/escap them . You can escape them without affecting
the semantics of the URL, but this is not recommended.

 

The third group of deprecated characters is the set of avoided characters

It is not wise to use them:
{ } | \ ^ [ ] `::Number 1 key before :: {8}
Reason for not wise: Gateways sometimes modify such characters, or use them as separators. This does not
It does not mean that the gateway will always modify these characters, but it may happen.
If you really want to use these characters, please escape them.

 

The fourth group of exception character sets

This set of characters consists of all ASCII control characters. The following characters are included including the space character:
< > # % " {5}
Control characters are non-printable US-ASCII characters (hex 00~1F and 7F)
if Use, please escape. Some characters # (hash) and % (percent) have special meaning in URL context, you
can treat them as reserved characters. Other characters in this set cannot be printed, so they Escaping is the only way to
express, the three characters < > " need to be escaped because these characters are usually used
to separate URLs in text


Reprint  the wonderful flowers – http://www.cnblogs.com/season-huang/ 

Simple and clear distinction between escape, encodeURI and encodeURIComponent

 

I. Introduction

There are too many articles about the difference between these 3 methods, but most of them are very confusing. This article attempts to talk about these three methods from a practical point of view.

 

Second, escape and they are not the same class

In a nutshell, escape encodes strings (and the other two are for URLs) in order to make them readable on all computers.
The effect after encoding is in the form of %XX or %uXXXX.
Among them, ASCII letters, numbers, @*/+, these characters will not be encoded, and the rest will be.
Most importantly, when you need to URL encode, please forget this method, this method is used for strings, not for URLs.
In fact, I haven't used this method in actual work, so I won't talk more about it.

 

Three, the most commonly used encodeURI and encodeURIComponent

URL encoding is a common thing, so these two methods should be paid special attention to in practice.
They are both encoded URLs, the only difference is the encoded character range, where
The encodeURI method does not encode the following characters ASCII letters, numbers, ~!@#$&*()=:/,;?+'
The encodeURIComponent method does not encode the following characters: ASCII letters, numbers, ~!*()'
So encodeURIComponent encodes a larger range than encodeURI.
In practical terms, encodeURIComponent will encode http:// to http%3A%2F%2F but encodeURI will not.
 
 

Fourth, the most important thing, when should I use what method

The difference is very clear above, let's talk about it from the actual example.
    
1. If it is just an encoded string and not related to the URL, then use escape.
 
2. If you need to encode the entire URL and then need to use this URL, use encodeURI.
 
for example
encodeURI("http://www.cnblogs.com/season-huang/some other thing");
编码后会变为
"http://www.cnblogs.com/season-huang/some%20other%20thing";

其中,空格被编码成了%20。但是如果你用了encodeURIComponent,那么结果变为

"http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2Fsome%20other%20thing"

看到了区别吗,连 "/" 都被编码了,整个URL已经没法用了。

  

3、当你需要编码URL中的参数的时候,那么encodeURIComponent是最好方法。

var param = "http://www.cnblogs.com/season-huang/"; //param为参数
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"
看到了把,参数中的 "/" 可以编码,如果用encodeURI肯定要出问题,因为后面的/是需要编码的。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326407009&siteId=291194637