2016-03-12 111 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());