Html 分隔 URL 中的值,而不是使用 &

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3738668/
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-08-29 04:32:31  来源:igfitidea点击:

separating values in a URL, not with an &

htmlparsingurlparametersurlencode

提问by at.

Each parameter in a URL can have multiple values. How can I separate them? Here's an example:

URL 中的每个参数都可以有多个值。我怎样才能把它们分开?下面是一个例子:

http://www.example.com/search?queries=cars,phones

So I want to search for 2 different things: cars and phones (this is just a contrived example). The problem is the separator, a comma. A user could enter a comma in the search form as part of their query and then this would get screwed up. I could have 2 separate URL parameters:

所以我想搜索两种不同的东西:汽车和电话(这只是一个人为的例子)。问题是分隔符,一个逗号。用户可以在搜索表单中输入一个逗号作为查询的一部分,然后这会被搞砸。我可以有 2 个单独的 URL 参数:

http://www.example.com/login?name1=harry&name2=bob

There's no real problem there, in fact I think this is how URLs were designed to handle this situation. But I can't use it in my particular situation. Requires a separate long post to say why... I need to simply separate the values.

那里没有真正的问题,事实上我认为这就是 URL 被设计来处理这种情况的方式。但是我不能在我的特定情况下使用它。需要一个单独的长帖子来说明原因......我需要简单地将值分开。

My question is basically, is there a URL encodable character or value that can't possibly be entered in a form (textarea or input) which I can use as a separator? Like a null character? Or a non-visible character?

我的问题基本上是,是否存在无法以我可以用作分隔符的形式(文本区域或输入)输入的 URL 可编码字符或值?像一个空字符?还是一个不可见的角色?

UPDATE: thank you all for your very quick responses. I should've listed the same parameter name example too, but order matters in my case so that wasn't an option either. We solved this by using a %00 URL encoded character (UTF-8 \u0000) as a value separator.

更新:感谢大家的快速回复。我也应该列出相同的参数名称示例,但在我的情况下顺序很重要,所以这也不是一个选项。我们通过使用 %00 URL 编码字符 (UTF-8 \u0000) 作为值分隔符解决了这个问题。

回答by Quentin

The standard approach to this is to use the same key name twice.

对此的标准方法是两次使用相同的键名。

http://www.example.com/search?queries=cars&queries=phones

Most form libraries will allow you to access it as an array automatically. (If you are using PHP (and making use of $_POST/GETand not reinventing the wheel) you will need to change the name to queries[].)

大多数表单库将允许您自动将其作为数组访问。(如果您正在使用 PHP(并且使用$_POST/GET而不是重新发明轮子),则需要将名称更改为queries[].)

回答by Mark Cidade

Just URL-encode the user input so that their commas become %2C.

只需对用户输入进行 URL 编码,以便他们的逗号变为%2C

回答by BalusC

You can give them each the sameparameter name.

您可以为它们每个提供相同的参数名称。

http://www.example.com/search?query=cars&query=phones

The average server side HTTP API is able to obtain them as an array. As per your question history, you're using JSP/Servlet, so you can use HttpServletRequest#getParameterValues()for this.

一般的服务器端 HTTP API 能够以数组的形式获取它们。根据您的问题历史,您使用的是 JSP/Servlet,因此您可以使用HttpServletRequest#getParameterValues()它。

String[] queries = request.getParameterValues("query");

回答by malonso

Why not just do something like "||"? Anyone who types that into a search area probably fell asleep on their keyboard :} Then just explode it on the backend.

为什么不做类似“||”的事情?任何在搜索区域中输入的人都可能在他们的键盘上睡着了 :} 然后在后端爆炸它。

回答by Rohrbs

Come up with your own separator that is unlikely to get entered in a query. Two underscores '__'for example.

想出你自己的分隔符,它不太可能被输入到查询中。'__'例如两个下划线。

回答by Vinay B R

easiest thing to do would be to use a custom separator like [!!ValSep!!].

最简单的方法是使用像 [!!ValSep!!] 这样的自定义分隔符。