I can’t seem to get my API call right. I am trying to make an API call to return an html page and store it in a variable. I have verified that once I get html into the ‘data’ varia
I’m not sure how that got sent before I completed it, but…
I have verified that once I get html into the ‘data’ variable that it then displays properly. Unfortunately, I keep getting the error below when I click the button. Can someone tell me what I am doing wrong? Or give me a different quote site with API calls that work with CodePen?
I keep replying to my own posts, but I want to make sure that anyone who answers me has the most current data.
I read elsewhere on the site about using “https://crossorigin.me” at the beginning of the URL and that did get me a response that displayed correctly. Unfortunately, like the poster where I found that, I get the same quote repeatedly. It seems that it is getting cached somewhere and that cached data is getting returned. The poster said something about adding “cache:false” to the command, but I’m not sure where to do that.
One tip for the forum, when you have additional details to add and you’re the only one posting so far, it is better to just edit the original post.
I scan through all the ‘help wanted’ posts, and when I see multiple replies, I usually skip it as I assume a robust discussion is taking place, not just a guy talking to himself I only caught this one because I was avoiding some other obligations so read the forum a little more closely
If the URL includes the string “callback=?” (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.
I had to use jsonp to get data from wikipedia api, so it may be required for the forismatic one too.
Try something like this: http://api.forismatic.com/api/1.0/?method=getQuote&format=html&lang=en&callback=?
An example JSON object you are getting with that request looks like this:
{"quoteText":"Try and fail, but don\'t fail to try.",
"quoteAuthor":"Stephen Kaggwa",
"senderName":"",
"senderLink":"",
"quoteLink":"http://forismatic.com/en/cdce03b323/"}
The error you have is telling you that json.forEach is not a function, which is true. The forEach() method applies to arrays, not single javascript objects. If you want to access things from the object, review what you learned about dot notation and bracket notation for navigating objects.
I copied the code from “Convert JSON Data to HTML” and I was thinking that it was cycling through key/value pairs in the returned object, not multiple objects in an array. That solved the problem, now I can go on to making it look nice.
Just wanted to say I’ve spent all week trying to figure out API’s and JSON and while all the other reading and codeacademy courses I’m sure helped, THIS thread is what broke it open for me. Thank you for asking the question and posting your code! Cheers.
-Dusty