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问题。我用谷歌搜索并设法找到上面设置的头解决方案,但现在我遇到了这个错误:

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


任何想法?

最佳答案

尝试使用cors中间件。

var cors = require('cors');

app.use(cors());

07-24 17:52
查看更多