【发布时间】:2017-03-21 00:09:32
【问题描述】:
我正在尝试使用这样的 CORS 模块配置在 Node Js 中全局设置 Access-Control-Allow-Credentials 标头
`var cors = require('cors');
var corsOptions = {
origin: true,
'Access-Control-Allow-Origin': 'localhost:1550'
'Access-Control-Allow-Credentials': 'true'
};
app.use(cors(corsOptions));`
这似乎不起作用,所以我也为尝试设置预检响应的路线设置了它。我是这样设置的
`router.options('/login', cors(corsOptions));`
我检查了 Chrome 开发者工具,发现 Access-Control-Allow-Origin 标头已设置,但未设置 Access-Control-Allow-Credentials。我需要这个标头,因为我在 Angular 中设置了 withCredentials = true。
为什么 Node JS 没有设置 Access-Control-Allow-Credentials 标头而设置了其他标头?
我知道这是因为我在控制台中收到以下错误
XMLHttpRequest cannot load http://www.localhost:1550/api/v1/auth/login. Response to preflight request doesn't pass access control check: Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials. Origin 'http://127.0.0.1:1550' is therefore not allowed access.
【问题讨论】:
标签: angularjs node.js cors preflight