POST Request with XMLHttpRequest
const xhr = new XMLHttpRequest();
xhr.open("POST", "https://...");
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
const body = JSON.stringify({
userId: 1,
title: "...",
completed: false
});
xhr.onload = () => {
if (xhr.readyState == 4 && xhr.status == 201) {
console.log(JSON.parse(xhr.responseText));
} else {
console.log(`Error: ${xhr.status}`);
}
};
xhr.send(body);