After get request url with special field is truncated

I encountered in the project:
The url in the get request has special character parameters such as: url = xxxx?name=#123&num=12&xxx=xx, which causes the name value to be empty and the #123&num=12&xxx=xx parameters to be passed to the background later. truncate.
Solution:
1) Change the get request to the post request
2) According to the actual situation of the project, if you have limited input of special characters, you can use encodeURIComponent(), escape(), encodeURI() to encode the string.

Extension:
When the parameter value contains special characters ?!=()#%&, the url will also be truncated.

Here is an explanation of several functions:
1 escape() function

Definition and usage The
escape() function encodes a string so that it can be read on all computers.

Syntax
escape(string)

Parameters Description
string Required. The string to be escaped or encoded.

Return
Value A copy of the encoded string. Some of these characters are replaced with hexadecimal escape sequences.

Description
This method does not encode ASCII letters and numbers , nor does it encode the following ASCII punctuation characters: - _ . ! ~ * ' ( ) . All other characters are replaced by escape sequences.




2 encodeURI() function
Definition and Usage
The encodeURI() function encodes a string as a URI.

Syntax
encodeURI(URIstring)

Parameters Description
URIstring Required. A string containing a URI or other text to encode.

Returns a copy of the value
URIstring with some characters replaced by hexadecimal escape sequences.

Description
This method does not encode ASCII letters and numbers , nor does it encode these ASCII punctuation marks: - _ . ! ~ * ' ( ) .

The purpose of this method is to fully encode the URI, so the following ASCII punctuation marks that have special meaning in the URI are not escaped by the encodeURI() function: ;/?:@&=+$,#

3 encodeURIComponent() function

Definition and usage
The encodeURIComponent() function encodes a string as a URI component.

Syntax
encodeURIComponent(URIstring)

Parameters Description
URIstring Required. A string containing the URI component or other text to encode.

Returns a copy of the value
URIstring with some characters replaced by hexadecimal escape sequences.

illustrate
This method does not encode ASCII letters and numbers , nor does it encode these ASCII punctuation marks: - _ . ! ~ * ' ( ) .

Other characters (eg: ;/?:@&=+$,# these punctuation marks used to separate URI components) are replaced by one or more hexadecimal escape sequences.

Tips and Notes
Tip : Note the difference between the encodeURIComponent() function and the encodeURI() function, which assumes that its argument is part of a URI (such as a protocol, hostname, path, or query string). So the encodeURIComponent() function will escape the punctuation that separates parts of the URI.

4 Summary:

Through the analysis of the three functions, we can know that: escape(), except for ASCII letters, numbers and specific symbols, escapes and encodes all incoming strings, so if you want to encode URLs, it is best to Do not use this method. And encodeURI() is used to encode the entire URI, because the legal characters in the URI will not be encoded. The encodeURIComponent method should be the most commonly used in encoding a single URIComponent (referring to request parameters), which can escape Chinese and special characters in the parameters without affecting the entire URL.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326946707&siteId=291194637