在strapi 4中设置cors

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

我使用 Strapi 4 作为本地主机,当我通过 url 添加新资产时,Cors 设置出现问题

图片上传出现CORS错误

Access to XMLHttpRequest at 'https://www.countrysideveterinaryclinic.org/sites/default/files/interesting-cat-facts.jpg' from origin 'http://localhost:1337' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这里的解决方案没有帮助

我的

middleware.js

module.exports = [
  'strapi::errors',
  'strapi::security',
  'strapi::poweredBy',
  {
    name: 'strapi::cors',
    config: {
      enabled: true,
      header: '*',
      origin: ['http://localhost:1337']
    }
  },
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];
javascript cors localhost strapi
1个回答
40
投票

我自己找到了解决方案,也许会对某人有帮助

middlewares.js

module.exports = [
  'strapi::errors',
  'strapi::security',
  'strapi::poweredBy', // here
  {
    name: 'strapi::cors',
    config: {
      enabled: true, // deprecated in v4.25.8
      headers: '*',
      origin: ['http://localhost:1337', 'http://example2']
    }
  },
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];

© www.soinside.com 2019 - 2024. All rights reserved.