JSFiddle - React, Tailwind, and code Playground
JavaScript
function sendXHR(url, method, payload, callback) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
console.log('Server response: ' + xhr.response);
callback(xhr.response);
} else {
alert(xhr.status + ': ' + xhr.statusText);
}
}
};
xhr.open(method, url);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.setRequestHeader('Accept', 'application/json');
xhr.responseType = 'json';
xhr.send(payload);
}