# Random Quote Machine API troubles

**URL:** <https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507>\
**Category:** General\
**Created:** [December 30, 2016, 2:21am UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507 "2016-12-30T02:21:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![reeveress](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@reeveress](https://forum.freecodecamp.org/u/reeveress)\
**Post date:** [December 30, 2016, 2:21am UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/1 "2016-12-30T02:21:39Z")

</div>

Hi all,

I’m trying to build the random quote generator and have trouble getting JSON object from the API. I’m using an extremely basic html and javascript just to see if the API is returning something and get nada when pressing button. Any ideas?

Link to CodePen: [http://codepen.io/reeveress/pen/XNvPMY](http://codepen.io/reeveress/pen/XNvPMY)

---

<div class="post-metadata">

**Author:** ![codefu-chivy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codefu-chivy/32/67050_2.png) [@codefu-chivy](https://forum.freecodecamp.org/u/codefu-chivy)\
**Post date:** [December 30, 2016, 4:00am UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/2 "2016-12-30T04:00:32Z")

</div>

Hey there! It looks like you haven’t added a script for jquery in the javascript settings so by default it won’t be recognized. Just a tip, use your console, it’s a really powerful tool for debugging. It was showing that “$” is not defined, a dead giveaway that the jquery syntax wasn’t being processed.

---

<div class="post-metadata">

**Author:** ![codefu-chivy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codefu-chivy/32/67050_2.png) [@codefu-chivy](https://forum.freecodecamp.org/u/codefu-chivy)\
**Post date:** [December 30, 2016, 4:02am UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/3 "2016-12-30T04:02:31Z")

</div>

Also one more thing. You’ve also set up your click handler to register click events on the quote div, not the button.

---

<div class="post-metadata">

**Author:** ![reeveress](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@reeveress](https://forum.freecodecamp.org/u/reeveress)\
**Post date:** [December 30, 2016, 5:42pm UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/4 "2016-12-30T17:42:56Z")

</div>

Thanks! Console has proven to be awesome. I didn’t realize that it outputs error messages. I fixed the two issues but now I get a new one: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

Google seems to tell me that it is a cross domain issue and for security reasons Chrome is blocking that request. Do you have any advice on what to use to go around this issue? CORS and jsonp are the terms I noticed being thrown around.

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [December 30, 2016, 5:50pm UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/5 "2016-12-30T17:50:01Z")

</div>

That happens when the API has external sources blocked or you’re not including an API key.

If you need a simple API I can publish mine.

---

<div class="post-metadata">

**Author:** ![reeveress](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@reeveress](https://forum.freecodecamp.org/u/reeveress)\
**Post date:** [December 30, 2016, 6:35pm UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/6 "2016-12-30T18:35:16Z")

</div>

That would be awesome. I’m currently using [this](http://forismatic.com/en/api/) API and it doesn’t say anything about a key…

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [December 30, 2016, 7:55pm UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/7 "2016-12-30T19:55:18Z")

</div>

There you go buddy [https://mysterious-sierra-49778.herokuapp.com/api/](https://mysterious-sierra-49778.herokuapp.com/api/)

You can either get all the quotations (quotes, it’s just to avoid conflicts with a reserved word) with a GET request on [https://mysterious-sierra-49778.herokuapp.com/api/quotations](https://mysterious-sierra-49778.herokuapp.com/api/quotations) or get a single quotation with a GET request on [https://mysterious-sierra-49778.herokuapp.com/api/quotations/ID](https://mysterious-sierra-49778.herokuapp.com/api/quotations/ID) where ID is a number between 1 and 10 (I’ve only created 10 quotes).

---

<div class="post-metadata">

**Author:** ![Em-Ant](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/em-ant/32/29078_2.png) [@Em-Ant](https://forum.freecodecamp.org/u/Em-Ant)\
**Post date:** [December 30, 2016, 9:50pm UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/8 "2016-12-30T21:50:00Z")

</div>

The forismatic APi responds to GET using JSONP ([https://en.wikipedia.org/wiki/JSONP](https://en.wikipedia.org/wiki/JSONP)) This particular API wants the callback to be named **jsonp**. You should add `&jsonp=?` to your request url. `format` should also be set to `jsonp`.

Try this `$.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&lang=en&jsonp=?", function(json){...})`

---

<div class="post-metadata">

**Author:** ![reeveress](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@reeveress](https://forum.freecodecamp.org/u/reeveress)\
**Post date:** [January 2, 2017, 2:45am UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/9 "2017-01-02T02:45:40Z")

</div>

@Em-Ant It works! Thanks kindly.  
@Oxyrus Awesome job on the API, Alan Watts was the man. How come yours works without JSONP?

---

<div class="post-metadata">

**Author:** ![gordon2012](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gordon2012/32/6509_2.png) [@gordon2012](https://forum.freecodecamp.org/u/gordon2012)\
**Post date:** [January 2, 2017, 3:05am UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/10 "2017-01-02T03:05:32Z")

</div>

You might also want to look into [CORS Anywhere] ([https://github.com/Rob--W/cors-anywhere](https://github.com/Rob--W/cors-anywhere)), a NodeJS proxy server that adds the required headers to requests. I spun up a version on heroku with my domain whitelisted for the production version of my projects, along with an embedded dev version for localhost.

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [January 2, 2017, 12:12pm UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/11 "2017-01-02T12:12:05Z")

</div>

I’m using a framework that provides JSON support out of the box 🙂

---

<div class="post-metadata">

**Author:** ![reeveress](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@reeveress](https://forum.freecodecamp.org/u/reeveress)\
**Post date:** [January 3, 2017, 12:52am UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/12 "2017-01-03T00:52:13Z")

</div>

Gotta say I had to look up every second word in your response but dang I learned a lot, thanks.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 1:08pm UTC](https://forum.freecodecamp.org/t/random-quote-machine-api-troubles/70507/13 "2021-01-16T13:08:01Z")

</div>


