由于“Access-Control-Allow-Origin”标头设置两次而出现 CORS 错误 - Strapi V4

问题描述 投票:0回答:1

由于

Access-Control-Allow-Origin
标头设置两次

,我收到以下错误

从源“https://xxx.yyy.com”访问“https://cms.yyy.com/api/data/all”的 XMLHttpRequest 已被 CORS 策略阻止:“Access-Control-Allow-” Origin' 标头包含多个值 'https://xxx.yyy.com,https://xxx.yyy.com',但只允许使用一个。

我使用 Strapi cms 作为我的后端数据。

strapi::cors
已添加到中间件中。我已经尝试过以下这些配置

1.

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

但是这些都没有解决问题。

strapi版本:- 4.15.5

cors strapi koa
1个回答
0
投票

您可以将其添加到 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',
];
© www.soinside.com 2019 - 2024. All rights reserved.