Submitting forms using the POST method in JavaScript

Let's now learn how to submit forms using the POST method. To do this, the method attribute of a form will be set to appropriate value:

<form action="/handler/" method="POST"> <input name="test1"> <input name="test2"> <input type="submit"> </form>

The difference between sending using the POST method is that the sent data will not be visible in the address bar of the browser. This is useful when the data is too long, or we don't want to show it to a user in the address bar.

The data sent by the POST method will end up on our server in the post property of the data object:

export default { '/handler/': function(data) { console.log(data.post); return 'test'; } }

For brevity, we can perform destructuring to get our data into a separate variable:

export default { '/handler/': function({post}) { console.log(post); return 'test'; } }

Ask a user for a login and password. After submitting, compare them with the login and password stored in variables on the server. If the data matches, print a success message, otherwise, a failure message.

enru