CORS issue in node.js failing

房东的猫 提交于 2019-12-24 15:32:54

问题


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

I use postman everything work fine but I got CORS issue when testing my ionic app which it runs at http://localhost:8100. I googled and manage to find above set header solution but now I'm getting this error :

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

Any thought?


回答1:


Try using the cors middleware.

var cors = require('cors');

app.use(cors());


来源:https://stackoverflow.com/questions/35953286/cors-issue-in-node-js-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!