Origin http://localhost is not allowed by Access-Control-Allow-Origin

后端 未结 5 850
臣服心动
臣服心动 2020-12-05 20:29

I\'m trying to do a fetch from backbone.js to my node.js server. However, I get the following error in the console:

Origin http://localhost is not allowed by A

5条回答
  •  有刺的猬
    2020-12-05 21:08

    This approach resolved my issue to allow multiple domain

    app.use(function(req, res, next) {
          var allowedOrigins = ['http://127.0.0.1:8020', 'http://localhost:8020', 'http://127.0.0.1:9000', 'http://localhost:9000'];
          var origin = req.headers.origin;
          if(allowedOrigins.indexOf(origin) > -1){
               res.setHeader('Access-Control-Allow-Origin', origin);
          }
          //res.header('Access-Control-Allow-Origin', 'http://127.0.0.1:8020');
          res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
          res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
          res.header('Access-Control-Allow-Credentials', true);
          return next();
        });
    

提交回复
热议问题