【发布时间】:2019-10-31 19:09:46
【问题描述】:
当我使用 NodeJS 服务器 REST API 时,我在 Angular 登录组件中遇到问题 连接数据库(MongoDB)
从源访问“http://localhost:3000/users/login”处的 XMLHttpRequest >“http://localhost:4200”已被 CORS 策略阻止:响应中 >“Access-Control-Allow-Credentials”标头的值为“Content-Type” ' >当请求的凭证模式为 'include' 时必须为 'true'。 XMLHttpRequest 发起的请求的 >credentials 模式由 >withCredentials 属性控制。
这是我在 nodejs 文件 (app.js) 中的代码
var cors = require ('cors');
app.use(cors({
origin:['http://localhost:4200','http://127.0.0.1:4200'],
credentials:true
}));
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', "http://localhost:4200");
res.header('Access-Control-Allow-Headers', true);
res.header('Access-Control-Allow-Credentials', 'Content-Type');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
next();
});
这里是 git 日志结果: https://prnt.sc/o38w12 此外,我提交时注册功能仍然有效
【问题讨论】: