Skip to content

Instantly share code, notes, and snippets.

@andris9
Created January 5, 2012 10:02
Show Gist options
  • Save andris9/1564522 to your computer and use it in GitHub Desktop.
Save andris9/1564522 to your computer and use it in GitHub Desktop.
Extremely simple AJAX call
function SimpleAJAX(url, body, callback){
if(!callback && typeof body=="function"){
callback = body;
body = undefined;
}
var http = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP");
http.open(body?'post':'get', url);
http.onreadystatechange = function(){
if(http.readyState == 4){
if(http.status == 200){
callback(null, http);
}else{
callback(new Error("Invalid response"));
}
}
};
body?http.send(body):http.send();
}
// Usage
SimpleAJAX("/post", "post body", function(error, response){
if(error){
alert("Error");
}else{
alert(response.responseText);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment