Skip to content

Instantly share code, notes, and snippets.

@srdjan-m
Last active June 6, 2016 10:24
Show Gist options
  • Save srdjan-m/237513da71f8cc84bfc950d013949324 to your computer and use it in GitHub Desktop.
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/
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