如何在获取 url 时强制网络浏览器使用 POST?
最佳答案
使用将 post 指定为方法的 HTML 表单:
<form method="post" action="/my/url/">
...
<input type="submit" name="submit" value="Submit using 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/url" onclick="submitAsPost(this.href); return false;">this is my post link</a>
如果您需要在服务器端强制执行此操作,您应该检查 HTTP 方法,如果它不等于 POST,则发送 HTTP 405 response code (method not allowed)返回浏览器并退出。具体如何实现取决于您的编程语言/框架等。
关于html - 获取 url 时如何强制 Web 浏览器使用 POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1651197/