Html 从 <A> 标签发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6180256/
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
POST from an <A> tag
提问by Carrotman42
Is it possible to do a POST from just an <a>
tag? I know anchor tags are usually just for GETs, and I know I can use javascript to do this (like in JavaScript post request like a form submit) but to me that seems a little messy. Is there a way to do this with straight HTML?
是否可以仅从<a>
标签进行 POST ?我知道锚标记通常只用于 GET,而且我知道我可以使用 javascript 来做到这一点(就像在JavaScript 发布请求中,如表单提交),但对我来说这似乎有点混乱。有没有办法用直接的 HTML 来做到这一点?
采纳答案by Oded
There is no way to POST
an a
element using only HTML.
有没有办法POST
在a
仅使用HTML元素。
As can be seen from this DTD fragment (HTML 4.01 spec):
从这个 DTD 片段(HTML 4.01 规范)可以看出:
<!ELEMENT A - - (%inline;)* -(A) -- anchor -->
<!ATTLIST A
%attrs; -- %coreattrs, %i18n, %events --
charset %Charset; #IMPLIED -- char encoding of linked resource --
type %ContentType; #IMPLIED -- advisory content type --
name CDATA #IMPLIED -- named link end --
href %URI; #IMPLIED -- URI for linked resource --
hreflang %LanguageCode; #IMPLIED -- language code --
rel %LinkTypes; #IMPLIED -- forward link types --
rev %LinkTypes; #IMPLIED -- reverse link types --
accesskey %Character; #IMPLIED -- accessibility key character --
shape %Shape; rect -- for use with client-side image maps --
coords %Coords; #IMPLIED -- for use with client-side image maps --
tabindex NUMBER #IMPLIED -- position in tabbing order --
onfocus %Script; #IMPLIED -- the element got the focus --
onblur %Script; #IMPLIED -- the element lost the focus --
>
There is no attribute that controls whether to use POST
or GET
with an a
element.
没有控制是否使用元素POST
或GET
与a
元素一起使用的属性。
You haveto script it, if you want to abuse the semantics.
如果您想滥用语义,您必须编写脚本。
回答by Ry-
Not really, no. You can, however, do something like this:
不是真的,不是。但是,您可以执行以下操作:
<form action="theUrl" method="POST">
<input type="hidden" name="param1" value="val" />
<input type="hidden" name="param2" value="val2" />
<a href="#" onclick="this.parentNode.submit()">Go to that link!</a>
</form>
You should find a better way, though. This one does not degrade gracefully.
不过,您应该找到更好的方法。这个不会优雅地降级。
回答by SLaks
You can use CSS to make an <input type="submit">
look like a hyperlink.
您可以使用 CSS 使<input type="submit">
外观看起来像一个超链接。
回答by nasatome
In case it serves somebody:
如果它为某人服务:
<a href="/your-route"
onclick="event.preventDefault();
document.getElementById('magic-form').submit();">
Magic Action
</a>
<form id="magic-form" action="/your-route"
method="POST" style="display: none;">
{{ csrf_field() }} <!-- from your framework -->
<input type="hidden" name="field1" value="value1" />
<!-- other fields -->
</form>
回答by tk_
Basically, you can't use an anchor tag for a POST request. However, there is a simple hack to achieve this.
基本上,您不能对 POST 请求使用锚标记。但是,有一个简单的技巧可以实现这一点。
<form id="myform" method="post" action="target.html"></form>
<a onclick="document.getElementById('myform').submit();">Submit to action</a>
I have tested this and already using it in our production code.
我已经对此进行了测试,并且已经在我们的生产代码中使用了它。
回答by MarioRicalde
Simple answer: no. You need to use javascript to do this kind of thing; since when you do a POST what you're doing is sending the data in the HTTP request. With get you're just sending it as part of the string (thus you can do it through the href
value).
简单的回答:没有。你需要使用javascript来做这种事情;因为当您执行 POST 时,您所做的是在 HTTP 请求中发送数据。使用 get 你只是将它作为字符串的一部分发送(因此你可以通过href
值来完成)。
回答by VGE
No clearly not without a javascript submit().
不显然不是没有 javascript submit()。
回答by Dan
No there's no way to do this without the use of scripting. Although you can use CSS to style a standard Submit button to look and act more like an tag.
不,如果不使用脚本,就无法做到这一点。尽管您可以使用 CSS 来设置标准提交按钮的样式,使其外观和行为更像一个标签。
回答by ecchymose
The 2 ways I think about are with JavaScript (as you said) or through server-scription.
我想到的两种方法是使用 JavaScript(如您所说)或通过服务器描述。
You'd basically send the GET request (with the A) to some server file, which would change the GET vars to POST and then re-submit instantly (redirect location). But that would mess the referer stats I think.
您基本上会将 GET 请求(带有 A)发送到某个服务器文件,这会将 GET 变量更改为 POST,然后立即重新提交(重定向位置)。但这会弄乱我认为的推荐人统计数据。
回答by Iryna Batvina
You should to add data-method="post"
您应该添加 data-method="post"
For example: <a href="/some-link" data-method="post">Logout</a>
例如: <a href="/some-link" data-method="post">Logout</a>