Skip to content

Instantly share code, notes, and snippets.

@ni5arga
Created April 3, 2023 04:14
Show Gist options
  • Save ni5arga/e3e87e851776c401e764da2988406ad6 to your computer and use it in GitHub Desktop.
Save ni5arga/e3e87e851776c401e764da2988406ad6 to your computer and use it in GitHub Desktop.
XML HTTP Request in JS
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