Skip to content

Instantly share code, notes, and snippets.

@ikuwow
Last active January 6, 2016 02:29
Show Gist options
  • Save ikuwow/9034c103768e75d37abc to your computer and use it in GitHub Desktop.
Save ikuwow/9034c103768e75d37abc to your computer and use it in GitHub Desktop.
ピュアなJSでAPIリクエストをするメモ(jsonとjsonp) ref: http://qiita.com/ikuwow/items/ed5f3c9ee0bd6147b7f3
(function(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState==4 && this.status==200) {
// responseをhogehogeする
}
};
xhr.responseType = 'json';
xhr.open('GET',endpoint,true);
xhr.send();
})();
(function(){
var requestAjax = function(endpoint, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState==4 && this.status==200) {
callback(this.response);
}
};
xhr.responseType = 'json';
xhr.open('GET',endpoint,true);
xhr.send();
};
})();
requestAjax("http://example.com/endpoint1", function(response){
// responseをhogehogeする
});
requestAjax("http://example.com/endpoint2", function(response){
// responseをhogehogeする
});
(function(){
var script = document.createElement('script');
script.src = 'https://api.instagram.com/v1/users/5001435/media/recent/?access_token='+accessToken+'&callback=instaCallback';
window.instaCallbackFavorites = function(response) {
// reponseをhogehogeする
};
document.body.appendChild(script);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment