Created
December 9, 2020 23:11
-
-
Save subramaniashiva/2e2f23cde6974724ec588d15155a80f1 to your computer and use it in GitHub Desktop.
This file contains 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
// 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