Created
April 4, 2014 18:29
-
-
Save remibreton/9980477 to your computer and use it in GitHub Desktop.
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
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
get("url", function success(data){ console.log(data) }, function error(data){ console.log(data) }, "json");