Question about APIs and outputting them

Question about APIs and outputting them
0.0 0

#1

I am still confused about certain aspects of looping through JSON data and then outputting it. This is what I ended up with:

$(document).ready(function(){

$("#newQuote").on(“click”, function(){
$.getJSON(“https://talaikis.com/api/quotes/random/”, function(data){

  $(".placeholder").html(data.quote);
 });

});
});

It works well and to me it seems sufficient, but all the documentation points to using a loop instead. Why use a loop if I only need to output one piece of data?


#2

There is a more simple way to do it with get.

var wiki = "https://en.wikipedia.org/w/api.php?action=opensearch&parse&origin=*&search=cats&format=json"

$.get(wiki, function(data){
data:‘jsonp’,
d = data;


#3

if you use the get function its short cut then just console.log(data) and you can always check if your getting JSON with a simple typeof. If your data returns string its not JSON. if it comes back object your golden


#4

The way you are calling the api right now, you are getting back one quote at a time. Reading his docs here ifyou are calling the api like :

$("#newQuote").on("click", function(){
$.getJSON("https://talaikis.com/api/quotes/", function(data){
 console.log(data);
 });
});  

you will get a json object containing 100 quotes within a single api call. Then you can iterate through it or Math.random, etc.