Skip to content

Instantly share code, notes, and snippets.

@nrobinson2000
Created April 23, 2017 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrobinson2000/faefbc2a7dea9071afef94d8be04ed97 to your computer and use it in GitHub Desktop.
Save nrobinson2000/faefbc2a7dea9071afef94d8be04ed97 to your computer and use it in GitHub Desktop.
Get a JSON object from API request
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