Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Last active January 9, 2019 09:10
Show Gist options
  • Save jiaaro/6427646 to your computer and use it in GitHub Desktop.
Save jiaaro/6427646 to your computer and use it in GitHub Desktop.
super simple implementation of getJSON
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();
};
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