2016-11-07 35 views
0

想设置接入控制允许证书中节点的js使用这样节点JS CORS模块不设置接入控制允许证书头部

`var cors = require('cors'); 
var corsOptions = { 
    origin: true, 
    'Access-Control-Allow-Origin': 'localhost:1550' 
    'Access-Control-Allow-Credentials': 'true' 
}; 

app.use(cors(corsOptions));` 

这并未”的CORS模块配置全局报头t似乎工作,所以我也为我设置预检响应的路线设置了它。我将它设置这样

`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.

回答

2

我认为您没有正确设置配置。如文档中

https://www.npmjs.com/package/cors#configuration-options

var corsOptions = { 
origin: 'localhost:1550', 
credentials : true 
} 

这将设置凭据头。由于Origin设置Access-Control-Allow-Origin和凭据设置了Access-Control-Allow-Credentials。

+0

谢谢,我现在意识到,我没有把文档彻底丢弃 –