CORS issue in same domain

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



CORS issue in same domain



I have an application which runs in my-dev.abc.com like URL in "abc" domain. (abc is not the real domain, it is for explaining the question).



When I tried to access the application portal.abc.com from the above mention application I'm getting following error message in the developer console and redirection won't happen.




Failed to load https://portal.abc.com/: Redirect from 'https://portal.abc.com/' to 'https://portal.abc.com/Account/Login?ReturnUrl=%2f' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://my-dev.abc.com' is therefore not allowed access.



Why is this CORS error happening even though both applications use the same domain name? How to resolve this? This redirection do from the Nodejs side. Client-side use angularjs. NodeJs redirection code snippet as shown below,




function redirectApp(req, res)
res.clearCookie(COOKIES.USER_NAME);
res.clearCookie(COOKIES.ROLES);
res.clearCookie(COOKIES.EMPLOYEE_ID);
res.redirect('https://portal.abc.com');





Possible duplicate of I thought cross-subdomain AJAX requests were allowed, but this Chrome error seems to indicate otherwise
– Orel Eraki
Aug 10 at 5:52





A subdomain is NOT considered the same domain, and for good reason.
– Derek
Aug 10 at 5:53





When making up domains for use in a question, use example.com.
– Brad
Aug 10 at 5:59


example.com




1 Answer
1



if you are using Express, try settings allowed host to enable access control, I had the same issue with django and spent hours trying to figure it out.




app.use(function(req, res, next)
var allowedOrigins = ['http://localhost:9000'];
var origin = req.headers.origin;
if(allowedOrigins.indexOf(origin) > -1)
res.setHeader('Access-Control-Allow-Origin', origin);

res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', true);
return next();
);





is this should do in the portal.abc.com application or my-dev.abc.com application?
– manoj b86
Aug 10 at 5:55





add your application domain in allowed hosts! in your case my-dev.abc.com
– Nadeem Shadan
Aug 10 at 6:05





my application is my-dev.abc.com and I'm trying to redirect to portal.abc.com . I need to add my-dev.abc.com as allowed host in portal.abc.com. am i right here?
– manoj b86
Aug 10 at 6:35






yes! that's what you need to do
– Nadeem Shadan
Aug 10 at 6:52






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS