javascript - how to allow cors for specific domain on express - Stack Overflow

I am trying to send a cookie after login via ajax from localhost to a server hosted somewhere else. TO

I am trying to send a cookie after login via ajax from localhost to a server hosted somewhere else. TO counter the error related to cookies, I use in my axios:

var instance = axios.create({ 
   withCredentials: true
});

and in expressjs I have this:

app.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();
});

So, I am getting this error now

Failed to load : Response to preflight request doesn't pass access control check: 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'. Origin 'http://localhost:8080' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

How can I tell express to whitelist my domain or how do I just send back the cookie so I can stay logged in?

To add more info, when I make a post (login) request to the expressjs, it returns a cookie, now my browser does not send the cookie by iself, so that is why I am usiing withCredentials

I am trying to send a cookie after login via ajax from localhost to a server hosted somewhere else. TO counter the error related to cookies, I use in my axios:

var instance = axios.create({ 
   withCredentials: true
});

and in expressjs I have this:

app.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();
});

So, I am getting this error now

Failed to load https://foo.herokuapp./login: Response to preflight request doesn't pass access control check: 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'. Origin 'http://localhost:8080' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

How can I tell express to whitelist my domain or how do I just send back the cookie so I can stay logged in?

To add more info, when I make a post (login) request to the expressjs, it returns a cookie, now my browser does not send the cookie by iself, so that is why I am usiing withCredentials

Share Improve this question asked Sep 13, 2017 at 12:51 n102866n102866 511 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You replace the * with the origin (http://localhost:8080 in your example).

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信