Help with JSON from API

Help with JSON from API
0

#1

I’m having some trouble retrieving a random quote from an API for my random quote generator. Could anybody point out what I’m doing wrong or provide any tips? Should I be using a different API?

I’m using this API: https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en

Here’s a link to my test CodePen: https://codepen.io/gabrielbruce/pen/qjXPzg

Here’s the pasted code (from a button press)

$(document).ready(function() {
$("#new-quote").on(“click”, function(){
$.getJSON(“https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en”, function(json) {
$(“div”).html(json.quoteText);
});
});
});


#2

If you check the browser console, you have a CORS error (you can google the error in the console for more info).

I usually solve it by adding ?callback=? at the end of the API URL. But it seems for this API, you have to do it like this https://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?, which should achieve the same result as callback=? (which converts the request to jsonp).


#3

Thank you so much!

I was starting to get a bit frustrated because it seemed like I had everything right. Now I’m off to read up on JSONP. Is this covered further in FCC?

20 internet points for you!


#4

No.

When something goes wrong, be sure to check the browser console and google any errors you see.