URL 通过Get方式传递数组参数
Posted caihemm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了URL 通过Get方式传递数组参数相关的知识,希望对你有一定的参考价值。
URL 通过Get方式传递数组参数
方法1:
?id=1&id=2&id=3
后台获取时,只需要reqeust.getParameterValues("id") 获取String数组。
http协议的要求
解析参数时,相同的key会覆盖前一个,
如果带[]会当成一维数组来处理,就不会覆盖了
直接可以url =url+"?str1="+arrayP[0]+"&str2="+arrayP[1];
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]
The request.getParameter("foo")
will also work on it, but it‘ll return only the first value.
String foo = request.getParameter("foo"); // value1
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
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
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]
以上是关于URL 通过Get方式传递数组参数的主要内容,如果未能解决你的问题,请参考以下文章