Created
December 28, 2016 07:09
-
-
Save avtaniket/3f8605f4cb7e8b1c21ac402ee2bc2f22 to your computer and use it in GitHub Desktop.
A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Request error : | |
| XMLHttpRequest cannot load https://subdomain.domain.com/api/campaign/auth/twitter. Response to preflight request doesn't pass access control check: A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'https://subdomain2.domain.com' is therefore not allowed access. | |
| Solution : | |
| var whitelist = ['https://subdomain.domain.com', 'https://subdomain2.domain.com']; | |
| // All api requests | |
| app.use(function (req, res, next) { | |
| var origin = req.headers.origin; | |
| if(whitelist.indexOf(origin) > -1){ | |
| res.header('Access-Control-Allow-Origin', origin); | |
| } | |
| // CORS headers | |
| //res.header("Access-Control-Allow-Origin", "*"); // restrict it to the required domain | |
| res.header("Access-Control-Allow-Credentials", true); | |
| res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); | |
| // Set custom headers for CORS | |
| res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key,If-Modified-Since,Authorization'); | |
| if (req.method == 'OPTIONS') { | |
| res.status(200).end(); | |
| } else { | |
| next(); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment