正在发生的事情是,当通过Angular $ http.post()请求从网站点击登录路线时,我无法成功进入Node API,但是当从邮递员测试时它确实很好用(其余的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});
});
正如您所看到的那样,用户似乎是真实的,但是当从角度调用api时,没有数据传递给重定向... O.o
答案 0 :(得分:2)
从您通过URL(在技术上用于GET请求)发送用户数据(用户名/密码)的网站,但您应该将其作为POST数据发送,因为它是POST请求。您将如何执行此操作:
$http.post("http://apiadress.demo/auth/login", {
username: $scope.username,
password: $scope.password
}).then( success, failure );