如何强制Web浏览器在获取URL时使用POST? [英] How do you force a web browser to use POST when getting a url?

查看:136
本文介绍了如何强制Web浏览器在获取URL时使用POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何强制Web浏览器在获取url时使用POST?使用指定post的HTML表单使用指定post的HTML表单作为方法:

 < form method =postaction =/ my / url /> 
...
< input type =submitname =submitvalue =使用POST提交/>
< / form>

如果您使其作为链接发生(不推荐) ,您可以让一个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 / urlonclick =submitAsPost(this.href); return false;>这是我的帖子链接< / a>

如果您需要在服务器端执行此操作,您应该检查HTTP方法,如果不是等于POST,将 HTTP 405响应代码(不允许的方法)发送回浏览器并退出。具体的实现方式取决于你的编程语言/框架等。


How do you force a web browser to use POST when getting a url?

解决方案

Use an HTML form that specifies post as the method:

<form method="post" action="/my/url/">
    ...
    <input type="submit" name="submit" value="Submit using POST" />
</form>

If you had to make it happen as a link (not recommended), you could have an onclick handler dynamically build a form and submit it.

<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.

这篇关于如何强制Web浏览器在获取URL时使用POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆