Skip to content

Instantly share code, notes, and snippets.

@remibreton
Created April 4, 2014 18:29
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 remibreton/9980477 to your computer and use it in GitHub Desktop.
Save remibreton/9980477 to your computer and use it in GitHub Desktop.
var get = function(url, callback, error, format){
if(!format) format = "json";
var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
if(typeof callback === "function") callback(format == "json" ? JSON.parse(xmlhttp.responseText) : xmlhttp.responseText);
} else {
if(typeof error === "function") error(format == "json" ? JSON.parse(xmlhttp.responseText) : xmlhttp.responseText);
}
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
@remibreton
Copy link
Author

get("url", function success(data){ console.log(data) }, function error(data){ console.log(data) }, "json");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment