In HTML is it possible for normal `` link to pr

2019-08-28 08:41发布

问题:

It is possible to use a HTML anchor link...

<a href="foo?bar=baz">link</a>

...to produce a GET request to foo with query params bar=baz.

Is it also possible to write the anchor link in such a way that it produces a POST request to foo with the params bar=baz encoded in the HTTP request body (like a form with method POST does) ?

回答1:

No

Alternatives: You can modify the request made by the anchor tag using javascript,

or

use a form, and emulate a anchor by styling the submit button to look like a link



回答2:

In the html file the anchor file doesn't support the POST values.

You can get the post values for html file only through the form tag.

<form action="test.php" method="post" > <input type="text" name="lname" /><br /> <input type="submit" value="Submit" /> </form>



回答3:

You can create a hidden form with the values you want to submit; and you link can trigger the submit() action on that form. This is easily done with jquery (or your favorite javascript library).

This is untested, but something like this should do it:

<form method="POST" id="the_form" action="foo/">
  <input type="hidden" name="bar" value="baz" />
</form>

<a href="#" id="submit_link">Send values</a>

<script type="text/javascript">
  $('#submit_link').click($('#the_form').submit());
</script>


回答4:

As Matt Ball commented, you can accomplish this with a form and no JS:

<form method="post" action="YOUR_ACTION">
    <input type="hidden" name="foo" value="bar" />
    <input type="submit" value="link" class="submitLink" />
</form>

and then style the submit to look like a regular link:

<style type="text/css">
input.submitLink {
    background: none;
    border: medium none;
    color: blue;
    cursor: pointer;
    margin: 0;
    padding: 0;
    text-decoration: underline;
}
</style>


标签: html http