6

i'm making cross domain request (client-side & server-side). in server.js i have added this code and cross domain req are working fine

function setAcceptsHeader(req, res, next) {
    'use strict';
    res.setHeader('Access-Control-Allow-Origin', '*');
    next();enter code here
}

app.options('*', function (req, res) {
    'use strict';
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    res.status(200).end();
});

Problem is when i added req.redirect() in server.js, im getting error XMLHttpRequest cannot load http://localhost:1214/#/about. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

app.get('/users/:email',setAcceptsHeader, function (req, res) {
    User.findOne({email:req.params.email}, function (err, post) {
      if(!post){
        res.json({error:'item not found'});
      }
      else{
        res.redirect('http://localhost:1214/#/about');//not working for me
      }
  });

I know at client side i can use window.location to route but i want this to be implemented at server side. help!!

jfriend00
  • 683,504
  • 96
  • 985
  • 979
navtej singh
  • 277
  • 3
  • 11
  • Are you including the CORS headers on the `/about/` page as well? – Karl Keefer Feb 25 '15 at 04:46
  • Cors not on page,and i dun know how to enable on page. its a template and and previously i was using angular routing, to route at "/about" but now i want this handeled by server, but res.redirect() giving cors error – navtej singh Feb 25 '15 at 05:01
  • Am not sure why redirect is not working. But on second point I think its better to handle redirect in ajax response in client side and just send the status code from the server. – Sami Feb 25 '15 at 09:14
  • The redirect is being blocked because the /about/ page has the wrong headers. Client side redirect I think is your answer here if you can't adjust cors on the /about/ page. – Karl Keefer Feb 26 '15 at 03:34

0 Answers0