Created
April 3, 2023 04:14
-
-
Save ni5arga/e3e87e851776c401e764da2988406ad6 to your computer and use it in GitHub Desktop.
XML HTTP Request in JS
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
const request = new XMLHttpRequest(); | |
request.open('GET', 'https://example.com/api/data', true); | |
request.onload = function() { | |
if (this.status >= 200 && this.status < 400) { | |
// Success! | |
const data = JSON.parse(this.response); | |
console.log(data); | |
} else { | |
// We reached our target server, but it returned an error | |
console.error('Server returned an error'); | |
} | |
}; | |
request.onerror = function() { | |
// There was a connection error of some sort | |
console.error('Connection error'); | |
}; | |
request.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment