【问题标题】:Setting cors in strapi 4在strapi 4中设置cors
【发布时间】:2022-06-18 21:35:58
【问题描述】:

我使用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.

Solution 从这里没有帮助

我的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',
];

【问题讨论】:

  • 您遇到了什么问题?如果您收到 CORS 错误消息,请将其添加到您的问题中。
  • 请注意,the documentation for Strapi's CORS middleware 提到了 headers(复数)属性,但没有提到 header(单数)属性。

标签: javascript cors localhost strapi


【解决方案1】:

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

middlewares.js

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

【讨论】:

    【解决方案2】:

    我正在使用“*”在 Google Cloud Platform、Cloud Shell 中运行,我认为它可以工作,但它没有。它仍然遇到相同的 CORS 错误

    module.exports = [
      'strapi::errors',
      'strapi::security',
      {
        name: 'strapi::cors',
        config: {
          enabled: true,
          headers: ['Content-Type', 'Authorization', 'Origin', 'Accept'],
          origin: '*'
        }
      },
      'strapi::poweredBy',...
    

    【讨论】:

      猜你喜欢
      • 2019-04-23
      • 2021-08-31
      • 1970-01-01
      • 2016-03-31
      • 2018-04-28
      • 1970-01-01
      • 2022-11-05
      • 2017-06-26
      • 2021-08-26
      相关资源
      最近更新 更多