Skip to content

Instantly share code, notes, and snippets.

@felixzapata
Created June 16, 2013 08:19
Show Gist options
  • Save felixzapata/5791368 to your computer and use it in GitHub Desktop.
Save felixzapata/5791368 to your computer and use it in GitHub Desktop.
JavaScript AJAX request
function ajax(url, opts){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
var completed = 4;
if(xhr.readyState === completed){
if(xhr.status === 200){
opts.success(xhr.responseText, xhr);
}else{
opts.error(xhr.responseText, xhr);
}
}
}
xhr.open(opts.method, url, true);
xhr.send(opts.data);
}
ajax('/foo', {
method: 'GET',
success: function(response){
console.log(response);
},
error: function(response){
console.log(response);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment