使用 HTTP Get 发送数组 [英] Send an Array with an HTTP Get
问题描述
如何使用 HTTP Get 请求发送数组?
How can i send an Array with a HTTP Get request?
我正在使用 GWT 客户端发送请求.
I'm Using GWT client to send the request.
推荐答案
这取决于目标服务器接受什么.对此没有明确的标准.另见 a.o.维基百科:查询字符串:
That depends on what the target server accepts. There is no definitive standard for this. See also a.o. Wikipedia: Query string:
虽然没有明确的标准,但大多数 Web 框架允许将多个值与单个字段相关联(例如 field1=value1&field1=value2&field2=value3
).
While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field (e.g.
field1=value1&field1=value2&field2=value3
).[4][5]
通常,当目标服务器使用强类型编程语言如 Java (Servlet),然后您可以将它们作为具有相同名称的多个参数发送.API 通常会提供专门的方法来获取多个参数值作为数组.
Generally, when the target server uses a strong typed programming language like Java (Servlet), then you can just send them as multiple parameters with the same name. The API usually offers a dedicated method to obtain multiple parameter values as an array.
foo=value1&foo=value2&foo=value3
String[] foo = request.getParameterValues("foo"); // [value1, value2, value3]
request.getParameter("foo")
也可以处理它,但它只会返回第一个值.
The request.getParameter("foo")
will also work on it, but it'll return only the first value.
String foo = request.getParameter("foo"); // value1
并且,当目标服务器使用弱类型语言如 PHP 或 RoR 时,您需要在参数名称后加上大括号 []
以触发返回值数组而不是单个值的语言.
And, when the target server uses a weak typed language like PHP or RoR, then you need to suffix the parameter name with braces []
in order to trigger the language to return an array of values instead of a single value.
foo[]=value1&foo[]=value2&foo[]=value3
$foo = $_GET["foo"]; // [value1, value2, value3]
echo is_array($foo); // true
如果您仍然使用 foo=value1&foo=value2&foo=value3
,那么它只会返回第一个值.
In case you still use foo=value1&foo=value2&foo=value3
, then it'll return only the first value.
$foo = $_GET["foo"]; // value1
echo is_array($foo); // false
请注意,当您将 foo[]=value1&foo[]=value2&foo[]=value3
发送到 Java Servlet 时,您仍然可以获得它们,但是您需要使用包括大括号在内的确切参数名称.
Do note that when you send foo[]=value1&foo[]=value2&foo[]=value3
to a Java Servlet, then you can still obtain them, but you'd need to use the exact parameter name including the braces.
String[] foo = request.getParameterValues("foo[]"); // [value1, value2, value3]
这篇关于使用 HTTP Get 发送数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!