How to make a POST request on clicking links?

How to make a POST request on clicking links? By clicking on the link, you need to serverauth post request to a specific URL. back-end for GOLANG
April 19th 20 at 12:33
2 answers
April 19th 20 at 12:35
A Go to do with it? It's in the frontend you need
April 19th 20 at 12:37
The link in the browser. So you need to do by means of the browser.

On pure JavaScript at the touch of a button

<a href="javascript:void(0)" click="sendPost()">Linkk</a>
the <script>
function sendPost() {
 var xhr = new XMLHttpRequest();
 var body = 'name=' + encodeURIComponent(name) +
 '&surname=' + encodeURIComponent(surname);

 xhr.open("POST", '/submit', true);
 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(body);
}
</script>


But most likely you probably have jQuery or some other library so you can use $.ajax

If you want to make the transition when you tap, you can create a hidden form and submitit it

<form href="/my/url" type="post" id="myForm">
 <input type="hidden" name=name" value="Sergey" />
</form>
<a href="javascript:void(0)" click="document.getElementById('myForm').submit()">Linkk</a>
@Johann67 , Yes, there is jQuery . If you do not mind, please provide an example of when the link is to a POST request to /example and go to that page - beverly.Streich commented on April 19th 20 at 12:40
<a href="javascript:void(0)" id="ny-link" >Linkk</a>
the <script>
$('#my-link').on("click", function(){
$.ajax({
 type: 'POST',
 url: '/example',
 data: data,
 success: function(e){},
 dataType: 'json'
});
})
</script>
- Johann67 commented on April 19th 20 at 12:43
@Johann67why something is not working, when clicking on the link, I see that invoked javascript:void(0), but the transition is not carried out - beverly.Streich commented on April 19th 20 at 12:46
@beverly.Streich, transition to where?
well, at least you formulate the question to fit your requirements - marcella_Mayert commented on April 19th 20 at 12:49
@marcella_Mayert, go to the page '/example'. I quite accurately, in my opinion, at the top States that want to implement. Read above: "when reference is to a POST request to /example and the go to this page") - beverly.Streich commented on April 19th 20 at 12:52
@beverly.Streich, POST does not lead to the change of address. Mozhnt after it is necessary to send GET? - marcella_Mayert commented on April 19th 20 at 12:55
@marcella_Mayert , I will be very grateful if you give an example how to do it. Preferably the complete code, because I'm not strong in JS. - beverly.Streich commented on April 19th 20 at 12:58
@beverly.Streich,
all of the user after successful submission of the post
for example
success: function(e){
 window.location = "http://javascript.ru"
},


Just not a very straightforward task. you need to send the post request and when uspeshniy processing to redirect the page to another address.
it is not clear. A redirect in this case it is better to do buck

ajax query Sergey Romanov wrote - marcella_Mayert commented on April 19th 20 at 13:01

Find more questions by tags JavaScript