Skip to content

Instantly share code, notes, and snippets.

@kn9ts
Created May 4, 2015 10:01
Show Gist options
  • Save kn9ts/686e10c2e1d6860613c6 to your computer and use it in GitHub Desktop.
Save kn9ts/686e10c2e1d6860613c6 to your computer and use it in GitHub Desktop.
How to Make AJAX Requests With Raw Javascript
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = isReady(function(xhr) {
console.log(xhr.responseText);
});
function isReady(callback) {
if (xhr.readyState < 4) {
return;
}
if (xhr.status !== 200) {
return;
}
// all is well
if (xhr.readyState === 4) {
callback(xhr);
}
}
xhr.open('POST', 'https://android.googleapis.com/gcm/send', true);
// xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Authorization", 'key=AIzaSyCl2AMo2itAacoNs3NZXPKF2_9NSAetkus');
xhr.send(JSON.stringify(message));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment