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

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

问题描述

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

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

请参阅 here ,举例。这个例子使用纯JavaScript,没有jQuery—你可以选择这个,如果你不想安装比你已经有的更多的东西。

 < form name =myform action =handle-data.phpmethod =post> 
< label for =query>搜寻:< / label>
< input type =textname =queryid =query/>
<按钮>搜寻< /按钮>
< / form>

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


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.

解决方案

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.

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天全站免登陆