Home

Published

- 1 min read

JavaScript POST request with XMLHttpRequest object

img of JavaScript POST request with XMLHttpRequest object

The solution for this is noted below

JavaScript POST request with XMLHttpRequest object

Solution

   let xhr = new XMLHttpRequest()
xhr.open('POST', 'https://reqbin.com/echo/post/json')
xhr.setRequestHeader('Accept', 'application/json')
xhr.setRequestHeader('Content-Type', 'application/json')

xhr.onreadystatechange = function () {
	if (xhr.readyState === 4) {
		console.log(xhr.status)
		console.log(xhr.responseText)
	}
}

let data = `{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}`

xhr.send(data)

Try other methods by searching on the site. That is if this doesn’t work