# No 'Access-Control-Allow-Origin' header is present on the requested resource

**URL:** <https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886>\
**Category:** General\
**Created:** [December 8, 2017, 6:16pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886 "2017-12-08T18:16:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![lupuselit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lupuselit/32/87299_2.png) [@lupuselit](https://forum.freecodecamp.org/u/lupuselit)\
**Post date:** [December 8, 2017, 6:16pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/1 "2017-12-08T18:16:49Z")

</div>

I’m doing the random quote machine chalenge using [https://quotesondesign.com/api-v4-0/](https://quotesondesign.com/api-v4-0/) I was getting the code on the page, then it stopped working and on the console i get this error:

> Failed to load [http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts\_per\_page]=1&callback=:](http://quotesondesign.com/wp-json/posts?filter%5Borderby%5D=rand&filter%5Bposts_per_page%5D=1&callback=:) No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘[http://127.0.0.1:3000](http://127.0.0.1:3000)’ is therefore not allowed access.

HTML

```auto
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Quote Machine Generator!</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  
  <section class="row">
    
    <p class="quote">Lorem ipsum dolor si sed amet libero beatae quod uhue gte odit.</p>
    <p class="author">- Emerson Lopes</p>
    <button class="new-quote" type="button" name="button">New quote</button>
    
  </section>
  
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script type="text/javascript" src="script.js"></script> 
  
  
</body>
</html>

```

JS

ps. dont mind js and jquery just in one file

```auto
// jshint esversion: 6
let body = document.querySelector("body");
let btn = document.querySelector(".new-quote");
let allchar = "0123456789ABCDEF";
let quote = document.querySelector(".quote");
let author = document.querySelector(".author");

btn.addEventListener("click", function(){

  // random color on click
  let randcol= "";
  for(let i = 0; i < 6; i++){
   randcol += allchar[Math.floor(Math.random()*16)];
}
  body.style.backgroundColor = "#"+randcol;

});

// let json = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1";
// let json2 = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=";

// JQUERY
$(document).ready(function() {
  
  $(btn).on("click", function(){
    
      $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) {
        $(quote).html(a[0].content);
        $(author).html(`- ${a[0].title}`);
      });
      
      // $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) {
      // $(body).append(a[0].content + "<p>— " + a[0].title + "</p>");
      // });
      
    

    
    // $(author).html('Someone really awesome!');
    
  });
});

```

---

<div class="post-metadata">

**Author:** ![Implaier](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/implaier/32/160235_2.png) [@Implaier](https://forum.freecodecamp.org/u/Implaier)\
**Post date:** [December 8, 2017, 6:53pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/2 "2017-12-08T18:53:01Z")

</div>

I’ve tried sending the request from chrome console, it worked fine.  
Try using codepen. Probably CORS issues occur because of local development.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [December 8, 2017, 6:55pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/3 "2017-12-08T18:55:07Z")

</div>

> [@emersonlbr](#):
>
> ps. dont mind js and jquery just in one file

jQuery is JS.

But anyway, when I load this into [codepen](https://codepen.io/kevinsmithwebdev/pen/xPogKx) (because it’s quicker), and it works fine.

But when I load it locally on my system, it doesn’t, same problem as you.

I have a quote [API test program](https://codepen.io/kevinsmithwebdev/pen/mMbMWB) that has the same issue. It crashes for CORS problems for _quotesondesign_ (but for some reason has no problem with _forismatic_.

CORS problems are always a pain in the butt. I would recommend doing some research there. I did some searching and found that Chrome doesn’t like dealing with CORS from localhost. So I tried Firefox, but same problem. So I tried IE and it worked.

I think that the problem here is because it is being locally served. Certain browsers have an issue with that. When you put this on the internet, it won’t be a problem. For now, You can use IE to test it locally. There are also ways to turn off the CORS protection in the other browsers, but that is too complicated for now.

The other issue is that your code, when it works, is “returning” the same quote every time - you need to prevent the browser from caching in the code.

---

<div class="post-metadata">

**Author:** ![lupuselit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lupuselit/32/87299_2.png) [@lupuselit](https://forum.freecodecamp.org/u/lupuselit)\
**Post date:** [December 8, 2017, 7:08pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/4 "2017-12-08T19:08:52Z")

</div>

> [@kevinSmith](#):
>
> jQuery is JS.

Yes that’s why it works fine together, I just said that cause some people like to keep then separated.

> [@kevinSmith](#):
>
> But anyway, when I load this into codepen (because it’s quicker), and it works fine.

oh! I will test this on codepen then, thanks!

---

<div class="post-metadata">

**Author:** ![lupuselit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lupuselit/32/87299_2.png) [@lupuselit](https://forum.freecodecamp.org/u/lupuselit)\
**Post date:** [December 8, 2017, 7:09pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/5 "2017-12-08T19:09:53Z")

</div>

> [@Implaier](#):
>
> Try using codepen. Probably CORS issues occur because of local development.

yup that’s kinda of a pain, I will try using codepen

---

<div class="post-metadata">

**Author:** ![Implaier](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/implaier/32/160235_2.png) [@Implaier](https://forum.freecodecamp.org/u/Implaier)\
**Post date:** [December 8, 2017, 7:39pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/6 "2017-12-08T19:39:19Z")

</div>

> **[indexzero/http-server](https://github.com/indexzero/http-server)**
>
> http-server - a simple zero-configuration command-line http server

This might help you out.

The problem is that popular browsers require a web server for cors requests.

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [December 8, 2017, 10:50pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/7 "2017-12-08T22:50:24Z")

</div>

Have you tried using `https://quotesondesign...` instead of `http`? You should get a response if you used https.

---

<div class="post-metadata">

**Author:** ![lupuselit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lupuselit/32/87299_2.png) [@lupuselit](https://forum.freecodecamp.org/u/lupuselit)\
**Post date:** [December 8, 2017, 10:59pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/8 "2017-12-08T22:59:28Z")

</div>

> [@Implaier](#):
>
> The problem is that popular browsers require a web server for cors requests.

I’m kind of beginner so I have no clue what you’re talking about xD

---

<div class="post-metadata">

**Author:** ![r1chard5mith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/r1chard5mith/32/160542_2.png) [@r1chard5mith](https://forum.freecodecamp.org/u/r1chard5mith)\
**Post date:** [December 9, 2017, 12:17am UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/9 "2017-12-09T00:17:28Z")

</div>

> [@kevinSmith](#):
>
> I think that the problem here is because it is being locally served. Certain browsers have an issue with that. When you put this on the internet, it won’t be a problem. For now, You can use IE to test it locally. There are also ways to turn off the CORS protection in the other browsers, but that is too complicated for now.

This. The only thing I have to add is that if you are accessing it with, ie; localhost:3000, you might try 127.0.0.1:3000 instead. I think sometimes it makes a difference.

---

<div class="post-metadata">

**Author:** ![lupuselit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lupuselit/32/87299_2.png) [@lupuselit](https://forum.freecodecamp.org/u/lupuselit)\
**Post date:** [December 9, 2017, 3:32pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/10 "2017-12-09T15:32:49Z")

</div>

> [@kevinSmith](#):
>
> I think that the problem here is because it is being locally served. Certain browsers have an issue with that. When you put this on the internet, it won’t be a problem. For now, You can use IE to test it locally. There are also ways to turn off the CORS protection in the other browsers, but that is too complicated for now.

I use a mac so I cant install IE, is it there any other browser I can test this? I already tried on Safari and Firefox and on Codepen I continue to get the same error.

> [@kevinSmith](#):
>
> The other issue is that your code, when it works, is “returning” the same quote every time - you need to prevent the browser from caching in the code.

oh thanks! that bug was bothering me a lot xD

---

<div class="post-metadata">

**Author:** ![anon52159105](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@anon52159105](https://forum.freecodecamp.org/u/anon52159105)\
**Post date:** [December 9, 2017, 4:08pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/11 "2017-12-09T16:08:25Z")

</div>

When it comes to CORS issues it’s often recommended to load the resource through [https://cors-anywhere.herokuapp.com](https://cors-anywhere.herokuapp.com)

---

<div class="post-metadata">

**Author:** ![lupuselit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lupuselit/32/87299_2.png) [@lupuselit](https://forum.freecodecamp.org/u/lupuselit)\
**Post date:** [December 9, 2017, 4:21pm UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/12 "2017-12-09T16:21:23Z")

</div>

Hey, after looking up the problem I just found out another API and it does not throw me any errors

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

interesting from the FCC forum as well:  
[https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/3](https://forum.freecodecamp.org/t/help-requested-for-random-quote-generator-solved/25206/3)

so it’s working now 😃 thanks to everyone who helped me!

---

<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, 11:33am UTC](https://forum.freecodecamp.org/t/no-access-control-allow-origin-header-is-present-on-the-requested-resource/162886/13 "2021-01-16T11:33:36Z")

</div>

Closed on January 16, 2021, 11:33am UTC


