Created
January 5, 2012 10:02
-
-
Save andris9/1564522 to your computer and use it in GitHub Desktop.
Extremely simple AJAX call
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
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