Help with Forismatic API/JSON GET Request

Hey guys, so I’m struggling with pulling data from the Forismatic API (http://forismatic.com/en/api/) for this project.

From the API documentation I know the data is formatted with tags like “quoteText” & “quoteAuthor” which is the data I require, however I can’t figure out how to pull this from the API.

In the link below you will see I’ve attempted to iterate through the JSON object and append the required info to the required div but so far I’m not having any joy.

It’s my first time using jQuery for a project so it’s very possible that I’ve made some glaring errors! Help would be greatly appreciated.

Thanks in advance!

http://codepen.io/Joshuaswift/pen/bpXoEo

Using this api call : http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en

I got the following response in JSON :
{“quoteText”:“It is not enough to have a good mind; the main thing is to use it well. “, “quoteAuthor”:“Rene Descartes “, “senderName”:””, “senderLink”:””, “quoteLink”:“http://forismatic.com/en/7d0c3ec445/”}

If you paste that response in a json validator it helps to visualize what you want to get out of it. Personally I use https://jsonformatter.curiousconcept.com/

In order to access the quote text and author I would try this
success:function(json){
//doSomethingWithIt//json.quoteText
//doSomethingWithIt//json.quoteAuthor
}

P.S. - It’s always helpful to just paste your api call into your browser to see what response you get to verify correct syntax.

2 Likes

Thanks that’s exactly what I was looking for!

Do you know of a way I can view the JSON data I’m pulling without using the chrome console? Unfortunately my current set up is an android tablet so I’m unable to use the console!

EDIT: Apologies I’ve only just seen your comment about pasting the API call into the browser to check the result, I had no idea this was possible thanks!

1 Like

No problem! Happy to help, I’m just a beginner as well so take everything I said with a grain of salt

You will not be able to change the inner HTML of an element of create a new HTML element and change its inner value using JSON due to Cross-Origin Resource Sharing (CORS) as your browser will block a cross-origin request. You will need to use the JSONP format to do what you are trying to achieve.

This is because browser security does not allow data exchange if the data is of different origin than you origin of your website.

Postman will help you see the JSON data clearly. It will give you an idea that what you need to include in you r app. Hope this will help you.

1 Like

Where do I find this link?

The Forismatic API:
http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en

Check this url
https://forismatic.com/ru/api/

1 Like

Thanks Claret! 1 month later hahaha

this is what worked for me: datatype jsonp with https request.

https://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?

otherwise, i was getting a “No ‘Access-Control-Allow-Origin’ header is present on the requested resource” error. this is something to with cross domain blocking by browser