如何在获取 url 时强制 Web 浏览器使用 POST?

编程入门 行业动态 更新时间:2024-10-27 04:29:26
本文介绍了如何在获取 url 时强制 Web 浏览器使用 POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在获取 url 时强制 Web 浏览器使用 POST?

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

推荐答案

使用指定 post 作为方法的 HTML 表单:

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>

如果您必须将其作为链接(不推荐)实现,您可以使用 onclick 处理程序动态构建表单并提交.

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>

如果你需要在服务器端强制执行,你应该检查 HTTP 方法,如果它不等于 POST,发送一个 HTTP 405 响应代码(方法不允许) 返回浏览器并退出.具体如何实施取决于您的编程语言/框架等.

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.

更多推荐

如何在获取 url 时强制 Web 浏览器使用 POST?

本文发布于:2023-11-01 20:41:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   如何在   Web   url   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!