将 POST 方法与 HTML 锚标记一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8398726/
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
Using the POST Method with HTML Anchor Tags
提问by dpsdce
I am certain the answer will be 'NO', but I wanted to ask anyway just incase I have missed something.
我确信答案是“不”,但我还是想问一下,以防万一我错过了什么。
Everyone knows that one pass data to a page in an anchor tag by using the GET method:
每个人都知道使用 GET 方法将数据传递到锚标记中的页面:
What I am wondering is if there was a way to do the same thing, but use the POST Method instead?
我想知道是否有办法做同样的事情,但改用 POST 方法?
My purpose in doing so is to keep the URLs the user sees clean by not putting anything in them that they do not need to see.
我这样做的目的是通过不放入任何他们不需要看到的内容来保持用户看到的 URL 干净。
This has nothing to do with security concerns as I already know there would be ways to obtain the data being passed.
这与安全问题无关,因为我已经知道有办法获取正在传递的数据。
If the answer is indeed no, then what methods do people use to pass data when there is a desire to keep the URLs clean? Cookies? Something else?
如果答案确实是否定的,那么当人们希望保持 URL 干净时,人们使用什么方法来传递数据?饼干?还有什么?
and how to deal with the scenarios when the URL length exceeds the permissible GET request length
以及如何处理URL长度超过允许的GET请求长度的场景
I am facing this issue while implementing sorting/pagination with displaytag, all the request parameters are appending in the sort/pagination url which is more then the permissible length of the GET request.
我在使用 displaytag 实现排序/分页时遇到了这个问题,所有请求参数都附加在排序/分页 url 中,这大于 GET 请求的允许长度。
回答by loscuropresagio
You could do something like this:
你可以这样做:
<form method="post" action="target.html">
<input type="hidden" name="name" value="value" />
<a onclick="this.parentNode.submit();">click here</a>
</form>
回答by BalusC
This behaviour is specific to display tag library. It allows for easily bookmarkable search results. If you really intend to change this to make use of POST, then you'd need to rewrite the display tag library or bring in some jQueryto manipulate the links.
此行为特定于显示标记库。它允许轻松添加书签的搜索结果。如果您真的打算更改它以使用 POST,那么您需要重写显示标记库或引入一些jQuery来操作链接。
The remnant of your questions boils nowhere. If you want GET (idempotent requests, bookmarkable URLs, searchbot-crawable URLs, etc), then use GET. If you want POST (non-idempotent requests, non-bookmarkable URLs, non-crawlable URLs, etc), then use POST.
你的问题的残余无处沸腾。如果您想要 GET(幂等请求、可添加书签的 URL、可搜索机器人抓取的 URL 等),请使用 GET。如果您想要 POST(非幂等请求、不可添加书签的 URL、不可抓取的 URL 等),请使用 POST。
Usually, POST is mandatory when the request can modifythe data in the server. Think of a SQL INSERT
, UPDATE
, DELETE
, etc. You certainly won't make this kind of requests GET. Imagine that you've a table with all "delete row" links which do GET and then a searchbot comes along...
通常,当请求可以修改服务器中的数据时,POST 是强制性的。想想 SQL INSERT
、UPDATE
、DELETE
等。您当然不会发出这种请求 GET。想象一下,您有一个包含所有“删除行”链接的表格,这些链接执行 GET,然后出现一个搜索机器人......
回答by PrasadB
You can use javascript. On onclick of link do form.submit
您可以使用 JavaScript。在点击链接时执行 form.submit
The only way I know of to deal with lenghty URL is to instead use POST.
我所知道的处理长 URL 的唯一方法是改用 POST。
回答by sunil jaiswal
It will work as post ,the name value can be through anchor tag and value of name="" can be access to $_POST[] globl var
它将作为 post 工作,名称值可以通过锚标记和 name="" 的值可以访问 $_POST[] globl var
回答by Selvakumar Ponnusamy
You may create a temporary form and submit it while onclick event of <a>
tag.
您可以创建一个临时表单并在<a>
标记的onclick 事件时提交它。