Skip to content

Instantly share code, notes, and snippets.

@dvidsilva
Created October 23, 2014 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvidsilva/0cdf7ba248e55c8c075b to your computer and use it in GitHub Desktop.
Save dvidsilva/0cdf7ba248e55c8c075b to your computer and use it in GitHub Desktop.
Creating an XHR request with JavaScript
function ajax() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 ) {
if(xmlhttp.status == 200){
document.getElementById("response").innerHTML = xmlhttp.responseText;
}
else if(xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert('something else other than 200 was returned');
}
}
}
xmlhttp.open("GET", "/ajax", true);
xmlhttp.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment