Skip to content

Instantly share code, notes, and snippets.

@gaurav-gogia
Created October 11, 2018 05:14
Show Gist options
  • Save gaurav-gogia/27cad55891650b55a930e2ef4e586434 to your computer and use it in GitHub Desktop.
Save gaurav-gogia/27cad55891650b55a930e2ef4e586434 to your computer and use it in GitHub Desktop.
Trying to perform ajax/xhr via vanillajs. Am I doing this right? If not then please advise. Thanks :)
const XMLHttpRequest = require('xhr2');
const xhr = new XMLHttpRequest();
xhr.open('GET', 'http://twitter.com/DesmondANIMUS/');
xhr.send(null);
xhr.onreadystatechange = function () {
var DONE = 4; // readyState 4 means the request is done.
var OK = 200; // status 200 is a successful return.
if (xhr.readyState === DONE) {
if (xhr.status === OK)
console.log(xhr.responseText); // 'This is the returned text.'
} else {
console.log('Error: ' + xhr.status); // An error occurred during the request.
}
console.log("Done");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment