javascript URL 查询字符串中的特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6750183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:43:12  来源:igfitidea点击:

Special Characters in URL query string

javascriptasp.nethtmlurlquery-string

提问by m0g

I have a situation where the user is able to enter any characters they want in a URL query string.

我有一种情况,用户可以在 URL 查询字符串中输入他们想要的任何字符。

Example:

例子:

http://localhost/default.aspx?ID=XXXX

http://localhost/default.aspx?ID=XXXX

http://localhost/default.aspx?ID=&XXXX

http://localhost/default.aspx?ID=&XXXX

http://localhost/default.aspx?ID=#XXXX

http://localhost/default.aspx?ID=#XXXX

The web page must accept the ID parameter as it is no matter what the characters are. However certain special characters such as ampersand(&) and pound(#) creates problems. How can I accept them as is?

网页必须接受 ID 参数,因为无论字符是什么。但是,某些特殊字符,例如与号 (&) 和磅 (#) 会产生问题。我怎样才能按原样接受它们?

采纳答案by Triton Man

If the user is entering the query string, they must properly encode the query string first. If you are creating the query string yourself, such as from a form submission, you will need to use a URL encode method.

如果用户正在输入查询字符串,他们必须首先正确编码查询字符串。如果您自己创建查询字符串,例如从表单提交中创建,您将需要使用 URL 编码方法。

回答by wanovak

This:

这:

encodeURIComponent(uri)

Where uriis the component after the ?ID=

uri后面的组件在哪里?ID=

回答by Muhammad Akhtar

Encode your URL HttpServerUtility.UrlEncode Method (String)

编码您的 URL HttpServerUtility.UrlEncode 方法(字符串)

Edit:following your comment, you want to get query String value of ID

编辑:按照您的评论,您想获取查询字符串值ID

 String id = Request.QueryString["ID"];

回答by bopjesvla

Use

利用

userinput = escape(userinput)

then, in PHP:

然后,在 PHP 中:

$userinput = urldecode($_GET['id'])

or in JS:

或在 JS 中:

userinput = unescape(userinput)