2016-03-12 59 views
0
app.use(function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8100'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', '*'); 

    // 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(); 
}); 

我用邮差一切工作正常,但我的测试,它运行在http://localhost:8100离子的应用程序时,我得到CORS问题。我google搜索并设法找到上述集头解决方案,但现在我得到这个错误:CORS问题node.js中失败

Request header field owner is not allowed by Access-Control-Allow-Headers in preflight response. 

任何想法?

+0

“[该'接入控制允许Headers'头不允许通配符。](http://stackoverflow.com/questions/13146892/cors-access-control-allow-headers-wildcard-被忽略)“ –

+0

@JonathanLonowski,我应该改变哪一部分? –

+1

除了[“简单的头文件”](https://fetch.spec.whatwg.org/#simple-header),你必须准确地列出哪些头文件应该被允许 - 例如''Owner,X-Requested用''代替'*'。 –

回答

0

尝试使用cors中间件。

var cors = require('cors'); 

app.use(cors());