繁体   English   中英

Reactjs:没有为反应包文件指定访问控制允许来源

[英]Reactjs: access-control-allow-origin not specified for react bundle file

在我的自定义 webpack 中,babel 生成了 React 应用程序,我创建了包文件。

从后端(节点),我正在发送一个 HTML 文件,该文件带有 script 标签,源标签引用frontend:3000/front_bundle.js 它非常适合自定义 webpack babel 应用程序。

但是,使用create-react-app 创建的React 应用程序,当我尝试从后端发送 html 文件时,脚本标记指向由 react-scripts 生成的包文件。

It says, access to script at http://localhost:2000/bundle.js from origin http://localhost:3000 has been blocked by CORS policy: NO "Access-control-Allow-Origin" header is present on the requested资源

我在节点中设置了 header,

app.use((req,res,next)=>{
   res.header("Access-control-allow-origin", "*")
   next();
});

也试过 app.use(cors()); 在节点。

在前端 public/index.html,我添加了 crossorigin="anonymous"。 在 package.json 中添加了一个代理,指的是 http://localhost:3000。 还是同样的问题。

关于如何解决这个问题的任何建议?

在我的自定义 webpack 中,babel 生成了 React 应用程序,我创建了包文件。

从后端(节点),我正在发送一个 HTML 文件,该文件带有 script 标签,源标签引用frontend:3000/front_bundle.js 它非常适合自定义 webpack babel 应用程序。

但是,使用create-react-app 创建的React 应用程序,当我尝试从后端发送 html 文件时,脚本标记指向由 react-scripts 生成的包文件。

It says, access to script at http://localhost:2000/bundle.js from origin http://localhost:3000 has been blocked by CORS policy: NO "Access-control-Allow-Origin" header is present on the requested资源

我在节点中设置了 header,

app.use((req,res,next)=>{
   res.header("Access-control-allow-origin", "*")
   next();
});

也试过 app.use(cors()); 在节点。

在前端 public/index.html,我添加了 crossorigin="anonymous"。 在 package.json 中添加了一个代理,指的是 http://localhost:3000。 还是同样的问题。

关于如何解决这个问题的任何建议?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM