Html POST 多个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/177815/
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
POST multiple parameters
提问by PhiLho
I am changing a GET to a POST. Currently I have .jsp?id=a,b,c,d. When changing this to a post I am still sitting the id parameter a,b,c,d . This is not working for me. Can I submit a comma separated list to a post parameter?
我正在将 GET 更改为 POST。目前我有 .jsp?id=a,b,c,d。将其更改为帖子时,我仍然使用 id 参数 a,b,c,d 。这对我不起作用。我可以将逗号分隔的列表提交给 post 参数吗?
回答by Fionn
You can do it like a select form input:
您可以像选择表单输入一样执行此操作:
url?param=value1¶m=value2¶m=value3
url?param=value1¶m=value2¶m=value3
Depending on you language and library you should be able to get an array of values for param.
根据您的语言和库,您应该能够获得 param 的值数组。
For example with asp.net mvc i do this to get an array of strings:
例如,对于 asp.net mvc,我这样做是为了获取一个字符串数组:
string[] values = Request.Form.GetValues("param");
回答by Olvagor
Fionn is right. Use
菲恩是对的。用
url?param=value1¶m=value2¶m=value3
to set multiple values to a single parameter. To read the values in your Servlet/JSP you can use
将多个值设置为单个参数。要读取 Servlet/JSP 中的值,您可以使用
String[] values = request.getParameterValues("param");
回答by PhiLho
Am I wrong or most of the answers are beside the point?
我错了还是大多数答案都离题了?
To answer precisely your question, yes, you can submit a comma separated list to a POST parameter. To be honest, I just did a quick try with a PHP script, but I don't see why Java would behave differently. One point with POST requests is precisely that you have much less constraints on syntax (no need to escape = & or such).
要准确回答您的问题,是的,您可以将逗号分隔的列表提交给 POST 参数。老实说,我只是快速尝试了一个 PHP 脚本,但我不明白为什么 Java 的行为会有所不同。POST 请求的一点是,您对语法的限制要少得多(无需转义 = & 等)。
So if you explain more in details what "doesn't work", perhaps we can help you more.
因此,如果您更详细地解释什么“不起作用”,也许我们可以为您提供更多帮助。
回答by random
Set to have your parameter deliver multiple values by naming it as an array. Instead of calling the input "id", call it "id[]"
通过将其命名为数组来设置参数传递多个值。而不是调用输入“id”,称之为“id[]”
<form method="post">
<select multiple name="id[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="send">
</form>
When processing, remember to extract the array of values sent by the named parameter.
处理时,记得提取命名参数发送的值数组。
回答by WR10
You can pass more than one parameter by using
您可以通过使用传递多个参数
xmlhttp
.send("files=" + files.value + "&tagcount="
+ tagcount.value);
Here filesand tagcountare the id of the parmeters to be passed. This was working for my Ajax Script. In normal cases you can just add the parameters by &. So the parameters can look like
这里的files和tagcount是要传递的参数的 id。这适用于我的 Ajax 脚本。在正常情况下,您可以通过&添加参数 。所以参数看起来像
"parameter1=" + parameter1.value + "¶meter2=" + parameter2.value
回答by Chris Lawlor
GET and POST have two different purposes. From the Wikipedia HTTP entry:
GET 和 POST 有两个不同的目的。来自维基百科 HTTP 条目:
GET
Requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause. See safe methods below.
POST
Submits data to be processed (e.g. from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.
得到
请求指定资源的表示。请注意,GET 不应用于导致副作用的操作,例如将其用于在 Web 应用程序中执行操作。一个原因是 GET 可能会被机器人或爬虫任意使用,它们应该不需要考虑请求应该引起的副作用。请参阅下面的安全方法。
邮政
将要处理的数据(例如,来自 HTML 表单)提交给标识的资源。数据包含在请求正文中。这可能会导致创建新资源或更新现有资源或两者兼而有之。
Basically, you should use GET to, well, GET information, and POST for any actions which alter the state of the server, such as adding new records.
基本上,您应该使用 GET 来获取信息,并将 POST 用于更改服务器状态的任何操作,例如添加新记录。
回答by user25778
in post request minimum 1 parameter should be there in parameter list. If you are appending parameter to request then it wont work. for that u need to send parameter as hidden field of form. Or try using following code
在 post 请求中,参数列表中应该有至少 1 个参数。如果您将参数附加到请求,则它将不起作用。为此,您需要将参数作为表单的隐藏字段发送。或尝试使用以下代码
function makePOSTRequest(url, parameters) {
函数 makePOSTRequest(url, 参数) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
}