Skip to content

Instantly share code, notes, and snippets.

@subramaniashiva
Created December 9, 2020 23:11
Show Gist options
  • Save subramaniashiva/2e2f23cde6974724ec588d15155a80f1 to your computer and use it in GitHub Desktop.
Save subramaniashiva/2e2f23cde6974724ec588d15155a80f1 to your computer and use it in GitHub Desktop.
// Create a new XMLHttpRequest Object
const xhr = new XMLHttpRequest();
// Initialize the object by calling open method
xhr.open('GET', 'https://images-api.nasa.gov/search?q=galaxy');
// Track the state changes of the request.
xhr.onreadystatechange = function () {
const DONE = 4; // readyState 4 means the request is done.
const OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
console.log(xhr.responseText); // 'This is the output.'
} else {
console.log('Error: ' + xhr.status); // An error occurred during the request.
}
}
};
// Send the request to initialized endpoint
xhr.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment