Created
May 4, 2015 10:01
-
-
Save kn9ts/686e10c2e1d6860613c6 to your computer and use it in GitHub Desktop.
How to Make AJAX Requests With Raw Javascript
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
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