Skip to content

Instantly share code, notes, and snippets.

@kn9ts
Created May 7, 2015 07:07
Show Gist options
  • Save kn9ts/d1f660d884dfe44c5be8 to your computer and use it in GitHub Desktop.
Save kn9ts/d1f660d884dfe44c5be8 to your computer and use it in GitHub Desktop.
Making an ajax request using plain JavaScript
var data = {
ids: [12, 18, 27, 35, 41, 53, 66, 68, 72, 85, 94, 103, 111, 120, 133]
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(xhr) {
if (xhr.readyState < 4 || xhr.status !== 200) {
return;
}
// all is well
if (xhr.readyState === 4) {
console.log(xhr.responseText); // the list of tweets
parseTweets(xhr.responseText)
}
}
xhr.open('GET', 'https://your_awesome_url.com/tweet/', true);
xhr.setRequestHeader("Authorization", 'key=oNs3NZXPKF2_9NSAetkus');
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(JSON.stringify(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment