Skip to content

Instantly share code, notes, and snippets.

@dickeylth
Forked from jiaaro/getjson.js
Created November 23, 2016 04:20
Show Gist options
  • Save dickeylth/ba3904d238ebf96a1f4b2b23878cee85 to your computer and use it in GitHub Desktop.
Save dickeylth/ba3904d238ebf96a1f4b2b23878cee85 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