I am using XMLHttpRequest to make a post request with JavaScript. I make the request and use the send()
method with an x-www-form-urlencoded string as the parameter. But when this request gets made in the browser, the paramters I passed through are not sent with the request.
Example: I send a post request
xhttp = new XMLHttpRequest();
xhttp.onload = function() {
//code
}
xhttp.open('POST', 'https://example.com/request', true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send('lorem=ipsum&title=title');
So the request gets sent but instead of being sent like: https://example.com/request?lorem=ipsum&title=title
It sends it like this: https://example.com/request
with none of the parameters.
CodePudding user response:
the xhttp.send()
's parameter is the request's body, not request's parameter.
So if the backend need parameters, the script does not send any parameters.
Move the lorem=ipsum&title=title
to xhttp.open('POST', 'https://example.com/request?lorem=ipsum&title=title', true);
Should work
xhttp = new XMLHttpRequest();
xhttp.onload = function() {
//code
}
xhttp.open('POST', 'https://example.com/request?lorem=ipsum&title=title', true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send();