简体   繁体   中英

Does Chrome changes Access-Control-Allow-Origin?

Browsing with Chrome to my nodejs web site. The Access-Control-Allow-Origin value that I see in Chrome developers tools is different from the one that I define in my nodejs

In my nodejs

res.setHeader('Access-Control-Allow-Origin', "siteA");

When I look at the Chrome developers tools under Network -> Response Headers I see

Access-Control-Allow-Origin: siteB

Tried with Edge - working fine

I clean the site data using Application->clean storage - didn't help

The error code I get in chrome is

Access to XMLHttpRequest at 'XXXXX' from origin 'siteA' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'siteB' that is not equal to the supplied origin.

Can you please change res.setHeader('Access-Control-Allow-Origin', "siteA"); to res.setHeader('Access-Control-Allow-Origin', "*");

 app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods","POST","GET")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,authorization,Accept");

  res.header('Access-Control-Allow-Credentials', 'true'); 
  next();
});  

Please copy and paste this code in your servers index.js file.I think this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM