Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS Error: "The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*'..."

I have a CORS error in NodeJS

"has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin'
header in the response must not be the wildcard '*' 
when the request's credentials mode is 'include'.
The credentials mode of requests initiated by the XMLHttpRequest
is controlled by the withCredentials attribute."

I put this in my server.js but I still got the error :

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://localhost:3000");
  res.header("Access-Control-Allow-Credentials", true);
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
  next();
});

Can somebody help me please ? Thanks

like image 581
VersifiXion Avatar asked Sep 03 '25 10:09

VersifiXion


1 Answers

This is a part of security, you cannot do that. If you want to allow credentials then your Access-Control-Allow-Origin must not use *. Still, if you want to allow for all domains, you can simply add req.header('Origin') instead of *.

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", req.header('Origin'));
  res.header("Access-Control-Allow-Credentials", true);
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE");
  next();
});
like image 70
Suresh Prajapati Avatar answered Sep 05 '25 00:09

Suresh Prajapati