使href(锚标签)请求POST而不是GET? [英] Making href (anchor tag) request POST instead of GET?

查看:728
本文介绍了使href(锚标签)请求POST而不是GET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<a href="employee.action" id="employeeLink">Employee1</a>

当我点击Employee1链接时,GET请求会进入服务器。我想使它POST,而不是GET请求。
有没有办法可以改变href的默认GET行为?

when i click the Employee1 link, GET request goes to server. I want to make it POST instead of GET request. Is there a way i can change default GET behaviour of href?

注意: - 我知道这可以在我们可以调用超链接的javascript函数的地方完成,然后创建表单并提交。但我正在寻找我们可以提到的
锚标签中的某些属性来发出POST请求,而不是GET请求吗?

Note:- I know it can be done where we can call javascript function on hyperlink click , then create form and submit it. But i am looking where we can mention some attribute in anchor tag to make POST request instead of GET request?

推荐答案

使用jQuery很简单,假设你想发布的URL位于同一台服务器上或者已经实现了CORS

Using jQuery it is very simple assuming the URL you wish to post to is on the same server or has implemented CORS

$(function() {
  $("#employeeLink").on("click",function(e) {
    e.preventDefault(); // cancel the link itself
    $.post(this.href,function(data) {
      $("#someContainer").html(data);
    });
  });
});

如果您坚持使用我强烈建议不支持的框架,请填写表格并提交链接

If you insist on using frames which I strongly discourage, have a form and submit it with the link

<form action="employee.action" method="post" target="myFrame" id="myForm"></form>

并使用(纯JS)

and use (in plain JS)

 window.onload=function() {
   document.getElementById("employeeLink").onclick=function() {
     document.getElementById("myForm").submit();
     return false; // cancel the actual link
   }
 }

使一个

 window.onload=function() {
   document.getElementById("employeeLink").onclick=function() {
     var myForm = document.createElement("form");
     myForm.action=this.href;// the href of the link
     myForm.target="myFrame";
     myForm.method="POST";
     myForm.submit();
     return false; // cancel the actual link
   }
 }

这篇关于使href(锚标签)请求POST而不是GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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