【发布时间】:2022-01-20 02:03:17
【问题描述】:
这就是我在 Node 中所拥有的:
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200/');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
我的前端是 Angular。一切正常,但突然间我开始收到 CORS 错误。当我检查控制台时,我得到了这个:
Initiator Context 和 Allowed Origin 完全相同。而且,正如我之前提到的,我遇到了 0 个问题,直到它突然决定停止工作。所以我不明白这里发生了什么。任何帮助将不胜感激。
【问题讨论】:
-
我猜您修改了从客户端发送的请求本身中的某些内容,并且该请求现在需要带有 OPTIONS 请求的pre-flight。向我们展示客户请求的详细信息,我们可以提供更多帮助。您可以通过更改内容类型或添加自定义标头或其他几种更改类型中的任何一种,无意中将请求从不需要预检更改为需要预检。
-
能否添加server/app.js文件,