Html 如何在获取 url 时强制 Web 浏览器使用 POST?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1651197/
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
How do you force a web browser to use POST when getting a url?
提问by Daniel Kivatinos
How do you force a web browser to use POST when getting a url?
如何在获取 url 时强制 Web 浏览器使用 POST?
采纳答案by Asaph
Use an HTML form that specifies post as the method:
使用指定 post 作为方法的 HTML 表单:
<form method="post" action="/my/url/">
...
<input type="submit" name="submit" value="Submit using POST" />
</form>
If you hadto make it happen as a link (not recommended), you could have an onclick handler dynamically build a form and submit it.
如果您必须使其作为链接发生(不推荐),您可以让一个 onclick 处理程序动态构建一个表单并提交它。
<script type="text/javascript">
function submitAsPost(url) {
var postForm = document.createElement('form');
postForm.action = url;
postForm.method = 'post';
var bodyTag = document.getElementsByTagName('body')[0];
bodyTag.appendChild(postForm);
postForm.submit();
}
</script>
<a href="/my/url" onclick="submitAsPost(this.href); return false;">this is my post link</a>
If you need to enforce this on the server side, you should check the HTTP method and if it's not equal to POST, send an HTTP 405 response code (method not allowed)back to the browser and exit. Exactly how you implement that will depend on your programming language/framework, etc.
如果您需要在服务器端强制执行此操作,您应该检查 HTTP 方法,如果它不等于 POST,则将HTTP 405 响应代码(方法不允许)发送回浏览器并退出。具体如何实现取决于您的编程语言/框架等。
回答by Jonathan Feinberg
<form method="post">
If you're GETting a URL, you're GETting it, not POSTing it. You certainly can't cause a browser to issue a POST request via its location bar.
如果你正在获取一个 URL,你是在获取它,而不是发布它。您当然不能让浏览器通过其地址栏发出 POST 请求。
回答by Ryan White
I have a feeling from your question you were just hoping to send a post request in a browser's address bar.
从你的问题中我有一种感觉,你只是希望在浏览器的地址栏中发送一个发布请求。
Just type the following into the address bar swapping the value for 'action' to the url that you like.
只需在地址栏中键入以下内容,将“action”的值交换为您喜欢的 url。
data:text/html,<body onload="document.body.firstChild.submit()"><form method="post" action="http://stackoverflow.com">
It's invalid html, but the browser's (at least all the ones i've tested it in so far) know what you mean, and I wanted to keep it as short as I could.
它是无效的 html,但浏览器(至少到目前为止我测试过的所有浏览器)都知道你的意思,我想尽可能缩短它。
If you want to post values, append as many inputs as you like, swapping name and value in each input for whatever you like.
如果您想发布值,请添加任意数量的输入,将每个输入中的名称和值交换为您喜欢的任何内容。
<input value="[email protected]" name="email">
<input value="passwordsavedinhistory" name="password">
It's important to note that sensitive information you post will be visible in:
请务必注意,您发布的敏感信息将在以下位置可见:
- your history
- your address bar
- your browser's autocomplete.
- possibly other sites that you visit from the same tab
- probably plenty of other things too
- 你的历史
- 你的地址栏
- 您浏览器的自动完成功能。
- 您可能从同一选项卡访问的其他网站
- 可能还有很多其他的东西
It's a really bad way to send a post request, and all the other answers are far better, but it's still cool that you can do it.
这是发送帖子请求的一种非常糟糕的方式,所有其他答案都好得多,但是您可以这样做仍然很酷。
回答by Ryan White
If you're trying to test something, I'd suggest using Fiddlerto craft your http requests. It will allow you to specify the action verb (GET, POST, PUT, DELETE, etc) as well as request contents. If you're trying to test a very specific request from a link but with POST instead, then you can monitor the requests your browser is making and reissue it only with the modified POST action.
如果您正在尝试测试某些内容,我建议您使用Fiddler来制作您的 http 请求。它将允许您指定动作动词(GET、POST、PUT、DELETE 等)以及请求内容。如果您尝试测试来自链接的非常具体的请求,但使用 POST 代替,那么您可以监控浏览器发出的请求,并仅使用修改后的 POST 操作重新发出请求。
回答by yeakbaba
回答by Gregory
The above submitAsPost() function is a good and elegant solution but it has a problem - if the URL is too long some browsers (including Firefox and IE) will return an error. Since many of us use POST in order to bypass this very limitation, I suggest this solution:
上面的 submitAsPost() 函数是一个很好且优雅的解决方案,但它有一个问题——如果 URL 太长,一些浏览器(包括 Firefox 和 IE)将返回错误。由于我们中的许多人使用 POST 来绕过这个限制,我建议使用以下解决方案:
// submit a URL using post
function submitAsPost(url) {
var bodyTag = document.getElementsByTagName('body')[0];
var postForm = document.createElement('form');
bodyTag.appendChild(postForm);
postForm.method = 'POST';
var serverAndParams = url.split("?");
postForm.action = serverAndParams[0];
var params = null;
try
{
var paramsAndHash = serverAndParams[1].split("#");
params = paramsAndHash[0];
var attrList = params.split("&");
for (var i = 0; i < attrList.length; i++)
{
try
{
var keyValue = attrList[i].split("=");
var el = document.createElement('input');
el.type="hidden";
el.name=keyValue[0];
var value = keyValue[1];
value = value.replace(/\+/g, ' ');
el.value=decodeURIComponent(value);
postForm.appendChild(el);
}
catch(error){}
}
}
catch(error){}
postForm.submit();
bodyTag.removeChild(postForm);
}
Tested with Firefox, Chrome and IE.
已使用 Firefox、Chrome 和 IE 进行测试。
回答by MarkZ
You can use a tool to test. I'm always asking the same question as you. There is quite a few tools available online. Here is the tool that I use: http://www.hurl.it/
您可以使用工具进行测试。我总是和你问同样的问题。网上有很多可用的工具。这是我使用的工具:http: //www.hurl.it/
回答by Rodney
This is a little late in the game but I ran across this and found that HTML 5 made some changes. You can use the input tag to add formmethod (thus selecting post). This worked for me.
这在游戏中有点晚了,但我遇到了这个问题,发现 HTML 5 做了一些更改。您可以使用 input 标签添加 formmethod(从而选择 post)。这对我有用。
see : http://www.w3schools.com/tags/att_input_formmethod.asp
回答by Adam
I know this question is old but someone may find this useful. You can use a command line tool like cURL (http://curl.haxx.se/) to post to a URL.
我知道这个问题很老,但有人可能会觉得这很有用。您可以使用 cURL ( http://curl.haxx.se/) 之类的命令行工具发布到 URL。
Example:
例子:
curl -v --basic --user username:password --request POST "http://www.theurltopostto.com"
回答by zardilior
If you had the problem of doing:
如果您遇到以下问题:
request.open('POST',url,true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send("data="+JSON.stringify(data));
and in dev tools you still se it is doing a GET then that means that your url is in the following format:
并且在开发工具中您仍然看到它正在执行 GET 那么这意味着您的 url 采用以下格式:
Meaning it should be:
意思应该是:
Its a very bizarre error maybe not related with your question but I ve had it a couple of times and it should be out there as it looks seriously dangerous. This happened to me when using an apache 2 server on Ubuntu 14.04, with not much configuration.
这是一个非常奇怪的错误,可能与您的问题无关,但我遇到过几次,它应该存在,因为它看起来非常危险。这发生在我在 Ubuntu 14.04 上使用 apache 2 服务器时,没有太多配置。