使用 POST 而不是 GET 创建链接 [英] Make a link use POST instead of GET

查看:36
本文介绍了使用 POST 而不是 GET 创建链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可能.但我想知道是否有人知道如何让超链接传递一些变量并使用 POST(如表单)而不是 GET.

I'm not sure if this is even possible. But I was wondering if anyone knows how to make a hyperlink pass some variables and use POST (like a form) as opposed to GET.

推荐答案

您创建一个带有隐藏输入的表单,其中包含要发布的值,将表单的 action 设置为目标 url,以及 post 的表单方法.然后,当你的链接被点击时,触发一个提交表单的 JS 函数.

You create a form with hidden inputs that hold the values to be posted, set the action of the form to the destination url, and the form method to post. Then, when your link is clicked, trigger a JS function that submits the form.

请参阅此处,以获取示例.这个例子使用纯 JavaScript,没有 jQuery —如果您不想安装任何已有的东西,您可以选择此项.

See here, for an example. This example uses pure JavaScript, with no jQuery — you could choose this if you don't want to install anything more than you already have.

<form name="myform" action="handle-data.php" method="post">
  <label for="query">Search:</label>
  <input type="text" name="query" id="query"/>
  <button>Search</button>
</form>

<script>
var button = document.querySelector('form[name="myform"] > button');
button.addEventListener(function() {
  document.querySelector("form[name="myform"]").submit();
});
</script>

这篇关于使用 POST 而不是 GET 创建链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆