我正在使用Node.js开发React项目。我在我的index.html文件中有两个外部引用:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
除此之外,我还有一个发送到服务器的POST请求,由于CSP,连接失败了。具体的POST请求错误如下:
对于外部链接,我遇到了一些与内容安全策略相关的错误,其中之一在下面展示。Refused to connect to 'localhost:3000/visitor' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.
我尝试了两种策略,但都没有解决我的问题:Refused to load the script 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
元数据绕过:
在index.html文件中所有脚本标签之前添加元数据,以基本上开放任何类型的外部文件通过脚本标签加载,但仍然在浏览器中得到相同的错误,并且这些JS文件无法在我的应用程序中加载。
- 我在node.js中使用了helmet模块,如下所示:
app.use(helmet({
contentSecurityPolicy: false,
}));
我应该怎么做才能解决这个问题,让应用程序能够正常运行?