-
-
Save dickeylth/ba3904d238ebf96a1f4b2b23878cee85 to your computer and use it in GitHub Desktop.
super simple implementation of getJSON
This file contains 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 getJSON(url, cb) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", url, true); | |
xhr.onreadystatechange = function() { | |
if (xhr.status != 200 || xhr.readyState != 4) return; | |
cb(JSON.parse(xhr.responseText)); | |
} | |
xhr.send(); | |
}; |
This file contains 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 getJSONP(url, cb_name, cb) { | |
var existing_cb = window[cb_name] || function(){}; | |
window[cb_name] = function() { | |
try { | |
cb.apply(this, arguments); | |
} catch(err) {} | |
existing_cb.apply(this, arguments); | |
} | |
var script = document.createElement("script"); | |
script.type = "text/javascript"; | |
script.src = url; | |
document.body.appendChild(script); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment