# Access-Control-Allow-Origin Policy

**URL:** <https://answers.netlify.com/t/access-control-allow-origin-policy/1813>\
**Category:** Support\
**Tags:** cors, authentication\
**Created:** [July 2, 2019, 3:17pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813 "2019-07-02T15:17:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![edel020](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/edel020/32/1576_2.png) [@edel020](https://answers.netlify.com/u/edel020)\
**Post date:** [July 2, 2019, 3:17pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/1 "2019-07-02T15:17:12Z")

</div>

Hi there,

I am trying to access json resources deployed to a standard netlify site - but getting:

" No ‘Access-Control-Allow-Origin’ header is present on the requested resource" .

From the SPA when I make a request to the json file.

I am trying to formulate a \_headers file to enable Cors - but having difficulty in getting this to work, and not sure this is in fact the correct approach.

I can see that the files are there as a standard browser request is retrieving them.

Any advice greatly appreciated!

Regards  
E.

---

<div class="post-metadata">

**Author:** ![edel020](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/edel020/32/1576_2.png) [@edel020](https://answers.netlify.com/u/edel020)\
**Post date:** [July 3, 2019, 6:07am UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/2 "2019-07-03T06:07:49Z")

</div>

Found the solution: this netlify.toml file will do the trick:

```
# The following redirect is intended for use with most SPAs that handle
# routing internally.
[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

[[headers]]
  # Define which paths this specific [[headers]] block will cover.
  for = "/*"
    [headers.values]
    Access-Control-Allow-Origin = "*"
```

---

<div class="post-metadata">

**Author:** ![fool](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/fool/32/37_2.png) [@fool](https://answers.netlify.com/u/fool)\
**Post date:** [July 4, 2019, 3:55pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/3 "2019-07-04T15:55:57Z")

</div>

great find and thanks so much for sharing it with us! Netlify’s tech support would have given you a very similar answer 🙂

If you’re using \_headers that would look like this instead:

```auto
/*
  Access-Control-Allow-Origin: *

```

and the main gotcha is that that needs to be sitting next to index.html after your build completes, rather than in the root of your repo like `netlify.toml`

---

<div class="post-metadata">

**Author:** ![Augus-He](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/augus-he/32/2246_2.png) [@Augus-He](https://answers.netlify.com/u/Augus-He)\
**Post date:** [August 8, 2019, 2:38am UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/4 "2019-08-08T02:38:09Z")

</div>

Thank U so much！！！It bothered me for two days.  
Ur solution works！😬

---

<div class="post-metadata">

**Author:** ![heshamelmasry77](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/heshamelmasry77/32/3203_2.png) [@heshamelmasry77](https://answers.netlify.com/u/heshamelmasry77)\
**Post date:** [September 30, 2019, 2:20pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/5 "2019-09-30T14:20:14Z")

</div>

I actually still have the same problem. I have an express app and This is my cors config :

and here is my server.js

```
'use strict';
const express = require('express');
const serverLess = require('serverless-http');
const app = express();
const bodyParser = require('body-parser');
const router = express.Router();
const mailer = require('../mailer');
const cors = require('cors');

const allowedOrigins = ['http://localhost:3000',
  'https://www.supercode.co.za'];
app.use(cors({
  origin: function(origin, callback){
    // allow requests with no origin
    // (like mobile apps or curl requests)
    if(!origin) return callback(null, true);
    if(allowedOrigins.indexOf(origin) === -1){
      let msg = 'The CORS policy for this site does not ' +
        'allow access from the specified Origin.';
      return callback(new Error(msg), false);
    }
    return callback(null, true);
  }
}));
router.post('/api/contact', (req, res) => {
  // console.log(req);
  const {
    firstName = req.body.firstName,
    lastName = req.body.lastName,
    message = req.body.message,
    phone = req.body.phone,
    email = req.body.email,
    website = req.body.website,
    company = req.body.company,
  } = req.body;
  mailer.send({email, firstName, text: message, phone, website, company, lastName}).then(() => {
    res.sendStatus(200);
  }).catch((error) => {
    console.log(error);
    res.sendStatus(500);
  });
});

```

also i have this in my toml file netlify  
[build]  
command = “npm install && npm run build”  
functions = “functions”  
[[headers]]  
# Define which paths this specific [[headers]] block will cover.  
for = “/_"  
[headers.values]  
Access-Control-Allow-Origin = "_”

---

<div class="post-metadata">

**Author:** ![futuregerald](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/futuregerald/32/33_2.png) [@futuregerald](https://answers.netlify.com/u/futuregerald)\
**Post date:** [October 2, 2019, 6:36pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/6 "2019-10-02T18:36:11Z")

</div>

@heshamelmasry77, you don’t have a value for the `Access-Control-Allow-Origin` header in your example, just empty quotes. You may want to enter a domain or \* in there. Also, are you trying to request a file from your node server or from your netlify site? you shared CORS settings on your server and on netlify so it’s not clear what the actual problem is. Please provide more details. Thanks.

---

<div class="post-metadata">

**Author:** ![andylemaire](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andylemaire/32/55345_2.png) [@andylemaire](https://answers.netlify.com/u/andylemaire)\
**Post date:** [October 3, 2019, 8:13am UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/7 "2019-10-03T08:13:26Z")

</div>

For some reason it’s not working for me… I have both **\_headers** and **netlify.toml** configured.

**\_headers**  
`/*`  
`Access-Control-Allow-Origin: *`

**netlify.toml**  
`[[headers]]`  
`for = "/*"`  
`[headers.values]`  
`Access-Control-Allow-Origin = "*"`

---

<div class="post-metadata">

**Author:** ![andylemaire](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andylemaire/32/55345_2.png) [@andylemaire](https://answers.netlify.com/u/andylemaire)\
**Post date:** [October 3, 2019, 8:34am UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/8 "2019-10-03T08:34:12Z")

</div>

It does work when I check it, but the CORS is still blocking [googletagmanager.com](http://googletagmanager.com)

---

<div class="post-metadata">

**Author:** ![futuregerald](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/futuregerald/32/33_2.png) [@futuregerald](https://answers.netlify.com/u/futuregerald)\
**Post date:** [October 4, 2019, 8:24pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/9 "2019-10-04T20:24:09Z")

</div>

@andylemaire Adding that header to your assets that load from Netlify will not have any effect on assets loaded from other places.

In my last response, I asked if you were trying to load a file from your server or netlify site and you didn’t provide any additional details so I still don’t know which file is giving you the CORS error.

---

<div class="post-metadata">

**Author:** ![andylemaire](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andylemaire/32/55345_2.png) [@andylemaire](https://answers.netlify.com/u/andylemaire)\
**Post date:** [October 4, 2019, 8:36pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/10 "2019-10-04T20:36:45Z")

</div>

@futuregerald I’m trying to load Google Tag Manager on my website hosted on Netlify.

---

<div class="post-metadata">

**Author:** ![andylemaire](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andylemaire/32/55345_2.png) [@andylemaire](https://answers.netlify.com/u/andylemaire)\
**Post date:** [October 8, 2019, 7:17am UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/11 "2019-10-08T07:17:59Z")

</div>

@futuregerald also, this used to work so I don’t understand why it’s not working anymore now…  
All I did was adjust my headers quite much.

**edit:** alright I fixed it, seems like I forgot something in the CSP, nothing to do with the `Access-Control-Allow-Origin`.

---

<div class="post-metadata">

**Author:** ![shkarlsson](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shkarlsson/32/3888_2.png) [@shkarlsson](https://answers.netlify.com/u/shkarlsson)\
**Post date:** [November 4, 2019, 9:48pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/12 "2019-11-04T21:48:35Z")

</div>

What was it? I have a similar problem and have been following your steps here without success.

---

<div class="post-metadata">

**Author:** ![perry](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/perry/32/16_2.png) [@perry](https://answers.netlify.com/u/perry)\
**Post date:** [November 5, 2019, 5:31pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/13 "2019-11-05T17:31:50Z")

</div>

@andylemaire - yes, please do tell us what you came up with for this!

---

<div class="post-metadata">

**Author:** ![andylemaire](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/andylemaire/32/55345_2.png) [@andylemaire](https://answers.netlify.com/u/andylemaire)\
**Post date:** [November 6, 2019, 9:58am UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/14 "2019-11-06T09:58:29Z")

</div>

@shkarlsson @perry I added all these to my CSP, not sure which was the blocking one but this made it work. So I’d compare it to yours…

connect-src [https://www.google-analytics.com](https://www.google-analytics.com);  
script-src [https://www.googletagmanager.com](https://www.googletagmanager.com) [https://www.google-analytics.com](https://www.google-analytics.com) [https://ssl.google-analytics.com](https://ssl.google-analytics.com) [https://www.googleadservices.com](https://www.googleadservices.com) [https://googleads.g.doubleclick.net](https://googleads.g.doubleclick.net);

---

<div class="post-metadata">

**Author:** ![prasanthsasikumar](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/prasanthsasikumar/32/5697_2.png) [@prasanthsasikumar](https://answers.netlify.com/u/prasanthsasikumar)\
**Post date:** [January 12, 2020, 3:51pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/15 "2020-01-12T15:51:43Z")

</div>

Thank you so much! It worked for me as well!

---

<div class="post-metadata">

**Author:** ![Karem1986](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/karem1986/32/14921_2.png) [@Karem1986](https://answers.netlify.com/u/Karem1986)\
**Post date:** [August 23, 2020, 10:21am UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/16 "2020-08-23T10:21:29Z")

</div>

I tried with this in my react app by creating the netlify toml file and does not work:  
my netlify toml code is

````auto
  # Define which paths this specific [[headers]] block will cover.
  for = "/*"
    [headers.values]
    Access-Control-Allow-Origin = "*"```

I removed the redirect part because i dont have /index.html 
please could you help me?![Screenshot from 2020-08-23 12-21-18|690x388](upload://nmmiW1bALgVhjxNrZbjcCTNUkxO.png)
````

---

<div class="post-metadata">

**Author:** ![jen](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/jen/32/6044_2.png) [@jen](https://answers.netlify.com/u/jen)\
**Post date:** [August 27, 2020, 5:12pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/17 "2020-08-27T17:12:06Z")

</div>

Hey @Karem1986,  
Could you please share a Netlify URL so we can take a look?

And just to make sure, does your full netlify.toml match this?

```auto
[[headers]]
   for = "/*"
   [headers.values]
      Access-Control-Allow-Origin = "*"

```

---

<div class="post-metadata">

**Author:** ![tad1693NS](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/tad1693ns/32/14757_2.png) [@tad1693NS](https://answers.netlify.com/u/tad1693NS)\
**Post date:** [October 9, 2020, 10:13pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/18 "2020-10-09T22:13:17Z")

</div>

Hi, I’m having the same issue, I’m using Gatsby + DatoCMS

At first this worked for me, but now it is not

> ```
> [[headers]]
> # Define which paths this specific [[headers]] block will cover.
> for = "/*"
> [headers.values]
> Access-Control-Allow-Origin = "*"
> 
> ```

this is the error I’m getting on my branch deploy  
`Access to fetch at 'https://site-api.datocms.com/docs/site-api-hyperschema.json' from origin 'https://dev.uthru.io' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:8000' that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.`

and this is toml file

```
    [build]
      functions = "functions/"

    [[redirects]]
      from = "/app/*"
      to = "/app"
      force = false
      status = 200

    [[headers]]
     # Define which paths this specific [[headers]] block will cover.
      for = "/*"
        [headers.values]
        Access-Control-Allow-Origin = "*"

```

---

<div class="post-metadata">

**Author:** ![luke](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/luke/32/39_2.png) [@luke](https://answers.netlify.com/u/luke)\
**Post date:** [October 15, 2020, 8:48am UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/19 "2020-10-15T08:48:23Z")

</div>

Hi, @tad1693NS. To troubleshoot this we need to be able to test. What is a URL where we will be able to see this error?

You can post that URL publicly or private message (PM) that to one of our support staff. I’ve confirmed that PMs are enabled for your community login. Note, that only one person can see the PM and this will likely mean a slower reply than posting the information publicly. Please feel free to reply to however you prefer though.

---

<div class="post-metadata">

**Author:** ![hccsit](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hccsit/32/20815_2.png) [@hccsit](https://answers.netlify.com/u/hccsit)\
**Post date:** [January 13, 2021, 2:30pm UTC](https://answers.netlify.com/t/access-control-allow-origin-policy/1813/20 "2021-01-13T14:30:14Z")

</div>

Have you resolved the CORS issue? I’m having exact issue even I have the headers section in my toml file

[Next page](https://answers.netlify.com/t/access-control-allow-origin-policy/1813.md?page=2)
