Pass a parameter containing '%' in URL?

2020-02-05 11:40发布

While passing my url, for example something:8000/something.jsp?param1=update&param2=1000&param3=SearchString%&param4=3 , I am getting following error:

Bad Request

Your browser sent a request that this server could not understand.

I know SearchString% which I need to pass as a parameter, has the issue. Then how to pass a parameter containing '%' in URL??

2条回答
在下西门庆
2楼-- · 2020-02-05 12:05

Use %25 in place of % In URLs % has a special meaning as an escape character

Special characters like (space) can be encoded like %20 (the ascii code for space/32 in hex)

Therefore a percent sign itself must be encoded using the hex code for % which happens to be 25

You can use http://www.asciitable.com/ to look up the appropriate hex code under the hx column

Alternatively, if you are doing this programatically (ie. with javascript) you can use the builtin function escape() like escape('%')

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-05 12:13

See this: Encode URL in JavaScript?

Basically you need to make sure the variables you are passing are encoded (the '%' character is a special character in URL encoding).

Any special characters - %,?,&, etc... need to be encoded. They are encoded with '%' and their hex number. So '%' should become '%25', '&' becomes '%26', etc.

Update: see When are you supposed to use escape instead of encodeURI / encodeURIComponent? for why you should avoid using escape.

查看更多
登录 后发表回答