Skip to content

Instantly share code, notes, and snippets.

@georgeOsdDev georgeOsdDev/index.html
Last active Dec 13, 2015

Embed
What would you like to do?
JSONPでデータ取得
<html>
<head>
<meta charset="utf-8">
<meta name=description content="JSONP SAMPLE">
<meta name=viewport content="width=device-width, initial-scale=1">
<title>JSONP</title>
</head>
<body>
<h2>JSONP Sample</h2>
<script src="./jsonp_client.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var jsonp = new JSONP();
jsonp.request("https://api.github.com",function(data){
console.log(data);
});
jsonp.request("https://api.github.com/legacy/repos/search/:javascript",function(data){
console.log(data);
});
</script>
</body>
</html>
(function(global){
global.jsonpCallbacks = {
index:0
};
function JSONP(){
this.s ={};
}
JSONP.prototype.request = function(url,cb){
var key = "_"+global.jsonpCallbacks.index++,
sep = "?",
self = this;
this.s[key] = document.createElement('script');
if (url.indexOf("?") > 0){
sep = "&";
}
this.s[key].src = url + sep + "callback=" + 'jsonpCallback'+key;
this.s[key].type ="text/javascript";
global["jsonpCallback"+key] = function(json){
self.s[key].parentNode.removeChild(self.s[key]);
delete global["jsonpCallback"+key];
cb(json);
};
document.getElementsByTagName('head')[0].appendChild(this.s[key]);
};
global.JSONP = JSONP;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.