I
I
impressive172020-04-17 16:33:33
JavaScript
impressive17, 2020-04-17 16:33:33

How to make a POST request on link click?

How to make a POST request on link click? On clicking the link, you need to send a post request to a specific URL. back-end on GOLANG

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Romanov, 2020-04-17
@Serhioromano

The link is in the browser. Means it is necessary to do means of the browser.
In pure javascript on the click of a button

<a href="javascript:void(0)" click="sendPost()">Linkk</a>
<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 have jQuery or some other library so you can use $.ajax
If you want to navigate on click you can create a hidden form and submit 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>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question