javascript - Multiple Access-Control-Allow-Origin headers being set - Stack Overflow

In my NodeExpress app, I am specifically removing any existing Allow-Origin header, and setting it to

In my Node/Express app, I am specifically removing any existing Allow-Origin header, and setting it to a specific domain. It works locally, but on the server, it keeps saying that the response header contains multiple values. This is the only place I set these headers in the entire codebase. Any thoughts on where else could be setting this?

'Access-Control-Allow-Origin' header contains multiple values 'https://*.mypany, *', but only one is allowed.

app.use(function(req, res, next){
    res.removeHeader('Access-Control-Allow-Origin');
    res.header('Access-Control-Allow-Origin', 'https://*.mypany'); 
    res.header('Access-Control-Allow-Headers', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
    if(req.method==='OPTIONS'){
        res.sendStatus(200);
    }
    next()
});

In my Node/Express app, I am specifically removing any existing Allow-Origin header, and setting it to a specific domain. It works locally, but on the server, it keeps saying that the response header contains multiple values. This is the only place I set these headers in the entire codebase. Any thoughts on where else could be setting this?

'Access-Control-Allow-Origin' header contains multiple values 'https://*.mypany, *', but only one is allowed.

app.use(function(req, res, next){
    res.removeHeader('Access-Control-Allow-Origin');
    res.header('Access-Control-Allow-Origin', 'https://*.mypany.'); 
    res.header('Access-Control-Allow-Headers', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
    if(req.method==='OPTIONS'){
        res.sendStatus(200);
    }
    next()
});
Share Improve this question asked Jun 11, 2020 at 19:10 DShultzDShultz 4,5415 gold badges36 silver badges55 bronze badges 6
  • What do you mean "existing Allow-Origin header"? What other part of your server is adding that? – jonrsharpe Commented Jun 11, 2020 at 19:12
  • I'm referring to the first line in the app.use function, I remove the 'Access--Control-Allow-Origin' header – DShultz Commented Jun 11, 2020 at 19:15
  • Yes, I can see that, but where do you think it's ing from to start with? – jonrsharpe Commented Jun 11, 2020 at 19:16
  • 2 there is probably a reverse proxy in front of your server, and for convenience they are just setting cors headers on everything – user120242 Commented Jun 11, 2020 at 19:20
  • Thanks for the idea - the issue was in the nginx config on the server being deployed to had the line: add_header 'Access-Control-Allow-Origin' '*' always; ...Though I'm surprised our removeHeader() statement didn't remove it. – DShultz Commented Jun 11, 2020 at 19:43
 |  Show 1 more ment

1 Answer 1

Reset to default 4

See if this explains it for you: https://developer.mozilla/en-US/docs/Web/HTTP/CORS/Errors/CORSMultipleAllowOriginNotAllowed. The problem is a browser will only accept one domain in an Access-Control-Allow-Origin header and you have a wildcard. The solution is to read the Origin header and echo that to Access-Control-Allow-Origin header on the response if it's an Origin you want to allow.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745252053a4618735.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信