2016-02-17 35 views
0

我在获取带有身份验证令牌的Cookie作为请求的一部分传递时遇到问题。当我在完成身份验证请求后查看Chrome时,会正确设置响应Cookie,但永远不会将其传递到其他请求中。Node + Angular:响应头未在请求中传递

/登录

快速路由生成的Cookie:

router.get('/login', 
    passport.authenticate('local', { 
    session: false 
    }), 
    function(req, res) { 
    console.log('/login authentication successful'); 
    token.generate(req.users, function(result) { 
     res.cookie('access_token', result, { 
     //secure : true, 
     //maxAge : 1000*60*60, // 1 hour 
     path: '/', 
     httpOnly: true 
     }); 
     res.json(Response.success(req.user)); 
    }); 
    } 
); 

我也有设置标头以下:

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"); 
    res.header("Access-Control-Allow-Credentials", true); 
    next(); 
    }); 

现在的角度,我传递一个配置对象,以我的$ http请求withCredentials:true,但到目前为止,没有任何工作。

$http({ 
    method: 'GET', 
    url: 'http://localhost:3000/api/v1/devices', 
    withCredentials: true 
}).then(function successCallback(response) { 
    // do stuff 
    }, function errorCallback(response) { 
    // oh noes! 
}); 

关于我的设置的说明:我有一台节点服务器使用browserSync显示端口8080上我已经看到了使用http://localhost:8080建议的客户方运行提供3000端口后端,与另一节点服务器而不是一个IP可能会导致cookie不被传递,但这似乎没有什么区别。

希望你们中的一个好人或女孩会对我的问题有个疑问。谢谢!

回答

0

似乎缺少Access-Control-Allow-Methods头,其中将包括动词使用"OPTIONS,POST,GET,PUT,DELETE"

参考MDN HTTP access control (CORS)

我不使用节点很多关于CORS但我知道有包,会做这一切你

相关问题