Created
April 23, 2017 15:58
-
-
Save nrobinson2000/faefbc2a7dea9071afef94d8be04ed97 to your computer and use it in GitHub Desktop.
Get a JSON object from API request
This file contains hidden or 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 httpGetJSON(url) // Get JSON object from an API | |
{ | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", url, false); // false for synchronous request | |
xhr.send( null ); | |
return JSON.parse(xhr.responseText); | |
} | |
// Example from https://api.github.com/ | |
// Get current_user_url | |
var value = httpGetJSON("https://api.github.com/"); | |
console.log(value.current_user_url); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment