No need to use jQuery, querystring
or manually assemble the payload. URLSearchParams
is a way to go and here is one of the most concise answers with the full request example:
fetch('https://example.com/login', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'param': 'Some value',
'another_param': 'Another value'
})
})
.then(res => {
// Do stuff with the result
});
Yes, you can use Axios or whatever you want instead of fetch
.
P.S. URLSearchParams
is not supported in IE.