I've been working on a project that involves pulling data from Goodreads' API in order to display the rating for a specific book. However, I've hit a bit of a snag.
Upon requesting data from this URL (
https://www.goodreads.com/book/review_counts.json?isbns=9781630586799&key=YOUR_KEY&callback=myCallback
), Goodreads sends back the following JSONP data:
myCallback([{
"id":22571333,
"isbn":"163058679X",
"isbn13":"9781630586799",
"ratings_count":657,
"reviews_count":2288,
"text_reviews_count":138,
"work_ratings_count":835,
"work_reviews_count":2596,
"work_text_reviews_count":155,
"average_rating":"3.93"
}])
Here is the javascript code I am using:
function myCallback(result) {
document.getElementById('goodreads-rating').innerHTML = result.average_rating;
}
var scriptTag = document.createElement('script');
scriptTag.src = "https://www.goodreads.com/book/review_counts.json?isbns=9781630586799&key=YOUR_KEY&callback=myCallback";
document.getElementsByTagName('head')[0].appendChild(scriptTag);
My goal is to retrieve the average_rating
from the data. However, when the HTML renders, all I see is the word "undefined".
I suspect that the issue lies in how I am handling the array/object within the response.
Can anyone offer some insight into what might be going wrong?