【发布时间】:2017-01-14 23:37:47
【问题描述】:
发生的情况是,当我通过 Angular $http.post() 请求从网站访问登录路由时,我无法成功登录到 Node API,但是在从邮递员(rest api测试应用程序)。
这是客户端代码(api调用):
function($http, $scope) {
$scope.$watch('search', function() {
fetch();
});
$scope.username = 'xxxxxxxxxxxx';
$scope.password = 'xxxxxxxxxxxxxxxx';
function fetch() {
$scope.details="";
$http.post("http://apiadress.demo/auth/login?username=" + $scope.username + "&password=" + $scope.password)
.then(function(response) {
$scope.details = response.data.state;
console.log(response);
});
}
}
此调用产生响应:
{
"state": "fail",
"user": null
}
当从 postma 客户端发出相同的调用响应时:
{
"state": "success",
"user": {
"_id": "xxxxxxxxxxxxxxxxxxxx",
"password": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"username": "xxxxxxxxxx",
"__v": 0,
"created_at": "2016-12-21T21:57:32.663Z"
}
}
我的服务器端路由代码是:
//log in
router.post('/login', passport.authenticate('login', {
successRedirect: '/auth/success',
failureRedirect: '/auth/failure'
}));
//responses whit successful login state
router.get('/success', function(req, res){
res.send({state: req.user ? 'success' : 'fail', user: req.user ?req.user : null});
});
如您所见,用户似乎已通过身份验证,但当从 Angular 调用 api 时,重定向中没有传递任何数据... O.o
【问题讨论】:
标签: javascript angularjs node.js api http