I have an AngularJS app which runs at http://localhost:9000. The front end app does a http request against http://localhost:8088. So, I had to setup the CORS on response headers.
I added on client side:
delete $http.defaults.headers.common["X-Requested-With"];
$http.defaults.headers.common["Accept"] = "application/json";
$http.defaults.headers.common["Content-Type"] = "application/json";
and on the server side (ExpressJS):
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
next();
});
Everything works fine while I am calling //localhost:9000 -> //localhost:8088. But, when I do a redirect to a different url (//api.example.com/authorize) I got the following error in browser console: XMLHttpRequest cannot load //api.example.com/authorize?response_type=code. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
next();
});
app.get('/authorize', function (req, res) {
var response = 'https://api.example.com/authorize?response_type=code';
res.redirect(response);
});
Do you have any clue what it is wrong?