I am trying to make a post request to a url with the following code . Passing object to http.send(params) function gives (400) bad request error. I am not able to trace the issue here .
var http = new XMLHttpRequest()
var url = 'http://somerandomurl'
http.open('POST', url, true)
http.setRequestHeader('content-type', 'application/json')
http.setRequestHeader('accept', 'application/json')
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
returndata = http.responseText
console.log(JSON.parse(returndata))
}
}
http.send(params)
Solution: http.send(JSON.stringify({'email': params.email, 'password': params.password})) it worked for me .
paramsobject is valid JSON? Your code works for me.