Getting CORS error due to `Access-Control-Allow-Origin` header getting set twice - Strapi V4

174 Views Asked by At

I am getting this following error due to Access-Control-Allow-Origin header getting set twice

Access to XMLHttpRequest at 'https://cms.yyy.com/api/data/all' from origin 'https://xxx.yyy.com' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'https://xxx.yyy.com,https://xxx.yyy.com', but only one is allowed.

I am using strapi cms for my backend data. strapi::cors is already added in middleware. I have already tried these following configs

1.

{
name: 'strapi::cors',
config: {
  origin: ['https://xxx.yyy.com'],
},
{
name: 'strapi::cors',
config: {
  origin: 'https://xxx.yyy.com',
},
{
name: 'strapi::cors',
config: {
  origin: "*",
},

but none of this solved the issue.

strapi version:- 4.15.5

enter image description here

1

There are 1 best solutions below

1
Sahib Khan On

you can add this to middleware.ts

    export default [
  'strapi::errors',
  'strapi::security',
  {
    name: "strapi::cors",
    config: {
      origin: http://localhost:1337,
      methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"],
      headers: ["Content-Type", "Authorization", "Origin", "Accept"],
      keepHeaderOnError: true,
    },
  },
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];