做href(锚标记)请求POST而不是GET?(复制)

时间:2022-11-27 17:33:52

This question already has an answer here:

这个问题已经有了答案:

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

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?

当我点击Employee1链接,获取请求到服务器。我想让它发布而不是收到请求。是否有办法改变href的默认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?

注意:-我知道可以在超链接点击时调用javascript函数,然后创建表单并提交。但是我正在寻找我们可以在锚标记中提到一些属性来发出POST请求而不是GET请求?

2 个解决方案

#1


21  

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

使用jQuery,假设您希望发布到的URL位于同一服务器上或者已经实现了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>

and use (in plain JS)

并使用(在普通JS中)

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

Without a form we need to make one

没有一种形式,我们需要做一个。

 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
   }
 }

#2


9  

To do POST you'll need to have a form.

要做帖子,你需要有一个表格。

<form action="employee.action" method="post">
    <input type="submit" value="Employee1" />
</form>

There are some ways to post data with hyperlinks, but you'll need some javascript, and a form.

有一些方法可以使用超链接发布数据,但是需要一些javascript和表单。

Some tricks: Make a link use POST instead of GET and How do you post data with a link

一些技巧:让一个链接使用POST而不是GET,以及如何使用link发布数据

Edit: to load response on a frame you can target your form to your frame:

编辑:要在一个框架上加载响应,你可以将你的表单定位到你的框架:

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

#1


21  

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

使用jQuery,假设您希望发布到的URL位于同一服务器上或者已经实现了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>

and use (in plain JS)

并使用(在普通JS中)

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

Without a form we need to make one

没有一种形式,我们需要做一个。

 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
   }
 }

#2


9  

To do POST you'll need to have a form.

要做帖子,你需要有一个表格。

<form action="employee.action" method="post">
    <input type="submit" value="Employee1" />
</form>

There are some ways to post data with hyperlinks, but you'll need some javascript, and a form.

有一些方法可以使用超链接发布数据,但是需要一些javascript和表单。

Some tricks: Make a link use POST instead of GET and How do you post data with a link

一些技巧:让一个链接使用POST而不是GET,以及如何使用link发布数据

Edit: to load response on a frame you can target your form to your frame:

编辑:要在一个框架上加载响应,你可以将你的表单定位到你的框架:

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