如何解决 Strapi 的 CORS 策略问题?
How to solve CORS policy problem for Strapi?
关于 CORS 策略的问题:帮助
描述
大家好!
我使用 Strapi 作为后端创建了一个 React 应用程序,但我不明白如何设置 CORS 策略。
Strapi 在我的在线服务器上,我 运行 React 应用程序在我的本地计算机上。当我尝试使用 Axios 发送请求时,出现了这个问题:
Access to XMLHttpRequest at 'http://xx.xx.xx.xx/players' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我在配置文件夹中创建了 middleware.js 文件,如文档中所述:https://strapi.io/documentation/v3.x/concepts/middlewares.html#configuration-and-activation
module.exports = {
load: {
before: ['timer', 'responseTime', 'logger', 'cors', 'responses', 'gzip'],
after: ['parser', 'router'],
},
settings: {
cors: {
enabled: true,
origin: ['*']
},
},
};
我已经查看了有关该主题的所有论坛和文章,我无法解决问题。
系统
- Node.js版本:v15.2.1
- Strapi 版本:3.3.2(社区)
我的错。当我将 Strapi 放在我的服务器上时,我更改了我的 React 应用程序中的 ip 地址,但忘记指定 Strapi 使用的端口。不要像我那样做! :/
关于 CORS 策略的问题:帮助
描述
大家好!
我使用 Strapi 作为后端创建了一个 React 应用程序,但我不明白如何设置 CORS 策略。 Strapi 在我的在线服务器上,我 运行 React 应用程序在我的本地计算机上。当我尝试使用 Axios 发送请求时,出现了这个问题:
Access to XMLHttpRequest at 'http://xx.xx.xx.xx/players' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我在配置文件夹中创建了 middleware.js 文件,如文档中所述:https://strapi.io/documentation/v3.x/concepts/middlewares.html#configuration-and-activation
module.exports = {
load: {
before: ['timer', 'responseTime', 'logger', 'cors', 'responses', 'gzip'],
after: ['parser', 'router'],
},
settings: {
cors: {
enabled: true,
origin: ['*']
},
},
};
我已经查看了有关该主题的所有论坛和文章,我无法解决问题。
系统
- Node.js版本:v15.2.1
- Strapi 版本:3.3.2(社区)
我的错。当我将 Strapi 放在我的服务器上时,我更改了我的 React 应用程序中的 ip 地址,但忘记指定 Strapi 使用的端口。不要像我那样做! :/