Last active
June 6, 2016 10:24
-
-
Save srdjan-m/237513da71f8cc84bfc950d013949324 to your computer and use it in GitHub Desktop.
javascript: vanilla example of ajax request. Taken from https://www.sitepoint.com/guide-vanilla-ajax-without-jquery/
This file contains hidden or 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.open('GET', 'send-ajax-data.php'); | |
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. | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment