# Help requested for Random Quote Generator \[Solved\]

**URL:** <https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206>\
**Category:** I Built This\
**Created:** [August 3, 2016, 10:16pm UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206 "2016-08-03T22:16:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcnshapiro](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcnshapiro/32/10414_2.png) [@marcnshapiro](https://forum.freecodecamp.org/u/marcnshapiro)\
**Post date:** [August 3, 2016, 10:16pm UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/1 "2016-08-03T22:16:17Z")

</div>

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

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [http://api.forismatic.com/api/1.0/?method=getQuote&format=html&lang=en](http://api.forismatic.com/api/1.0/?method=getQuote&format=html&lang=en). (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

---

<div class="post-metadata">

**Author:** ![marcnshapiro](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcnshapiro/32/10414_2.png) [@marcnshapiro](https://forum.freecodecamp.org/u/marcnshapiro)\
**Post date:** [August 3, 2016, 10:26pm UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/2 "2016-08-03T22:26:36Z")

</div>

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?

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [http://api.forismatic.com/api/1.0/?method=getQuote&format=html&lang=en](http://api.forismatic.com/api/1.0/?method=getQuote&format=html&lang=en). (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

I have spent about 4 hours on this and I am sure that the answer is simple, but it is eluding me.

My CodePen URL is:

```
 http://codepen.io/mshapiro/full/zBJVJL?editors=1111
```

---

<div class="post-metadata">

**Author:** ![marcnshapiro](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcnshapiro/32/10414_2.png) [@marcnshapiro](https://forum.freecodecamp.org/u/marcnshapiro)\
**Post date:** [August 4, 2016, 12:02am UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/3 "2016-08-04T00:02:37Z")

</div>

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](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.

Any ideas?

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [August 4, 2016, 1:47am UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/4 "2016-08-04T01:47:06Z")

</div>

Gimme a second to look at the previous post.

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 😉

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [August 4, 2016, 1:49am UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/5 "2016-08-04T01:49:46Z")

</div>

Ok, so you are referring to this post:

> **[What to know before beginning 'Random Quote Machine'](https://forum.freecodecamp.org/t/what-to-know-before-beginning-random-quote-machine/9815/14)**
>
> Ok, so after 30 mins of struggling, I found that I can add a “cache:false” line into my $ajax function. I think my browser cashes my request. I’ve been working for 2 days just to get the button to work haha I’m so glad I can move on to the...

What code have you got so far, do you have a Codepen link for it? Have you used a $ajax request or a $getJSON one?

---

<div class="post-metadata">

**Author:** ![IsaacAbrahamson](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/isaacabrahamson/32/30882_2.png) [@IsaacAbrahamson](https://forum.freecodecamp.org/u/IsaacAbrahamson)\
**Post date:** [August 4, 2016, 2:12am UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/6 "2016-08-04T02:12:23Z")

</div>

From the jQuery documentation for $.getJSON:  
[http://api.jquery.com/jquery.getjson/](http://api.jquery.com/jquery.getjson/)

> #JSONP

> 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=?`

This should take care of the CORS error!

---

<div class="post-metadata">

**Author:** ![charlieb410](https://avatars.discourse-cdn.com/v4/letter/c/e68b1a/32.png) [@charlieb410](https://forum.freecodecamp.org/u/charlieb410)\
**Post date:** [August 4, 2016, 4:58am UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/7 "2016-08-04T04:58:17Z")

</div>

Is there a specific reason why you are using a CORS proxy? Making the api call on [http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=](http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=)? using jquery getJSON will give you a nice json file that you can get the data from.

---

<div class="post-metadata">

**Author:** ![marcnshapiro](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcnshapiro/32/10414_2.png) [@marcnshapiro](https://forum.freecodecamp.org/u/marcnshapiro)\
**Post date:** [August 4, 2016, 9:40pm UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/8 "2016-08-04T21:40:03Z")

</div>

OK. It’s closer to working now. If I use the following code:

```
 $.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?", function(json) {
   $("#quoteText").html(JSON.stringify(json);
 }); 

```

then it displays the returned JSON and it changes with each button press.

If, however, I try to loop through the returned JSON as shown in “Convert JSON Data to HTML” with this code:

```
  $.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?", function(json) {
    var html = "";
    // Only change code below this line.
    json.forEach(function(val) {
      var keys = Object.keys(val);
      html += "<div>";
      keys.forEach(function(key) {
        html += "<strong>" + key + "</strong>: " + val[key] + "<br>";
      });
      html += "</div><br>";
    }); // Only change code above this line.
     
    $("#quoteText").html(html);
  });   

```

then it halts with the error:

```
 TypeError: json.forEach is not a function

```

and does not update the div with anything.

What am I doing wrong now? Is there some other library that I should add?

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [August 5, 2016, 12:02am UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/9 "2016-08-05T00:02:11Z")

</div>

> [@marcnshapiro](#):
>
> TypeError: json.forEach is not a function

An example JSON object you are getting with that request looks like this:

```auto
{"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](https://www.freecodecamp.com/challenges/accessing-objects-properties-with-the-dot-operator) and [bracket notation](https://www.freecodecamp.com/challenges/accessing-objects-properties-with-bracket-notation) for navigating objects.

---

<div class="post-metadata">

**Author:** ![marcnshapiro](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marcnshapiro/32/10414_2.png) [@marcnshapiro](https://forum.freecodecamp.org/u/marcnshapiro)\
**Post date:** [August 5, 2016, 12:52am UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/10 "2016-08-05T00:52:25Z")

</div>

Thank you, Jackson.

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.

---

<div class="post-metadata">

**Author:** ![DustyBest](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@DustyBest](https://forum.freecodecamp.org/u/DustyBest)\
**Post date:** [October 19, 2016, 9:26am UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/11 "2016-10-19T09:26:25Z")

</div>

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

---

<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:** [June 1, 2021, 6:04pm UTC](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/12 "2021-06-01T18:04:40Z")

</div>

Closed on June 1, 2021, 6:04pm UTC


