Created
October 20, 2021 03:53
-
-
Save zazaulola/b79c0d2c2cd4f3a646fba3a9a8ee9c04 to your computer and use it in GitHub Desktop.
Send Xhr-request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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