Skip to content

Instantly share code, notes, and snippets.

@zazaulola
Created October 20, 2021 03:53
Show Gist options
  • Save zazaulola/b79c0d2c2cd4f3a646fba3a9a8ee9c04 to your computer and use it in GitHub Desktop.
Save zazaulola/b79c0d2c2cd4f3a646fba3a9a8ee9c04 to your computer and use it in GitHub Desktop.
Send Xhr-request
function send(method, url, headers = {}, data = null) {
return new Promise((resolve, reject) => {
console.log(data);
const xhr = new XMLHttpRequest();
xhr.onload = () => resolve(xhr);
xhr.onerror = error => reject(error);
xhr.open(method, url, true);
console.log(xhr);
Object.entries(headers).forEach(([header, value]) => xhr.setRequestHeader(header, value));
xhr.send(data);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment