Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making href (anchor tag) request POST instead of GET? [duplicate]

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

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?

like image 745
emilly Avatar asked Jan 03 '14 14:01

emilly


People also ask

Can we use POST method in anchor tag?

This behaviour is specific to display tag library. It allows for easily bookmarkable search results. If you really intend to change this to make use of POST, then you'd need to rewrite the display tag library or bring in some jQuery to manipulate the links. The remnant of your questions boils nowhere.

How can I submit a POST form using the A HREF tag?

You can use href=”#top” or href=”#” to link to the top of the current page. To use the anchor tag as submit button, we need the help of JavaScript. To submit the form, we use JavaScript . submit() function.

How do I send a POST request with a tag?

There is no way to POST an a element using only HTML. There is no attribute that controls whether to use POST or GET with an a element. You have to script it, if you want to abuse the semantics.

Can we pass parameters in anchor tag like using POST method?

The only way to pass data with an anchor tag is to put them in the query string.


2 Answers

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> 

and use (in plain JS)

 window.addEventListener("load",function() {    document.getElementById("employeeLink").addEventListener("click",function(e) {      e.preventDefault(); // cancel the link      document.getElementById("myForm").submit(); // but make sure nothing has name or ID="submit"    });  }); 

Without a form we need to make one

 window.addEventListener("load",function() {    document.getElementById("employeeLink").addEventListener("click",function(e) {      e.preventDefault(); // cancel the actual link      var myForm = document.createElement("form");      myForm.action=this.href;// the href of the link      myForm.target="myFrame";      myForm.method="POST";      myForm.submit();    });  }); 
like image 67
mplungjan Avatar answered Sep 21 '22 11:09

mplungjan


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.

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

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

<form action="employee.action" method="post" target="myFrame"> 
like image 32
rkawano Avatar answered Sep 18 '22 11:09

rkawano