ExpressJS:请求已被CORS策略阻止:请求的资源上不存在“ Access-Control-Allow-Origin”标头

时间:2019-03-14 18:16:14

标签: node.js express next.js

在我的浏览器开发人员工具栏中,我收到以下关于POST请求的错误消息:

Access to XMLHttpRequest at 'http://localhost:8000/api/tags/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

但是,当我查看server.js时,我却允许访问:

app.prepare().then(() => {
    const server = express();

    server.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        next();
    });

有人知道为什么现在阻止它

1 个答案:

答案 0 :(得分:1)

最好按照以下方式使用“ cors”软件包。

as_adj