$http请求在node js上-如何成功返回值$http.post
$http request on node js - how to return a value in success $http.post
我正在编写angular + node js应用程序,并且我有一个登录方法,使用http post调用验证用户和密码:
login.js
$scope.userLogin = function (info) {
$http.post('/authenticate_user', {'name':info.name,'password':info.password})
.success(function(data) {
})
.error(function(data) {
console.log('Error:'+ data);
});
}
我正在处理身份验证的server.js文件中的:
app.post('/authenticate_user', function(req, res){
authenticate_user(req, res);
});
var authenticate_user=function(req, res){
var query= user_details.find({'name': req.body.name});
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate, max-age=0");
query.exec( function(err, docs){
if (docs.length==0) return false;
res.json(docs[0].password==req.body.password);
});
}
我想有userLogin函数在我的login.js文件有app.post()方法的值..
之类的 $http.post('/authenticate_user', {'name':info.name,'password':info.password})
.success(function(data) {
// what is data includes here? the response for the http post?
// or what the function I calling to aka authenticate_user, returns - in this case - a boolean value?
})
我想从我的api后的值被转移到在login.js文件的调用方法..
你知道怎么做吗?
谢谢
返回一个承诺而不是隐藏它
$scope.userLogin = function (info) {
return $http.post('/authenticate_user', info);
}
使用userLogin({name: '', password: ''}).then(function(success) {
console.log(success)
})
相关文章:
- $http服务未触发成功或错误回调
- Angular Cross Origin Request CORS失败,但节点http.get()成功返回
- 在http成功调用上启用锚标记无效
- 如何在 http 调用成功时禁用锚标记
- 承诺中的成功回调不会发生$http
- jQuery成功,访问HTTP状态文本
- Angular http在Phone gap中返回错误,但在浏览器中成功
- 当调用成功时,如何从angular 2中的http调用返回数据
- Angularjs自定义服务变量未被$http成功定义
- 无法解析来自 jquery 中成功的 HTTP HEAD 请求的标头
- 成功之间的区别,然后随着决心,承诺,http在angularjs中
- 拒绝$http成功的承诺
- 如何将值传递给 AngularJS $http成功回调
- 在AngularJS中的$http成功回调之外无法访问$scope变量值
- AngularJS: $http成功回调的作用域问题
- AngularJS如何在依赖提供商的$http成功函数中设置变量
- 为什么$http成功了't返回更新后的承诺
- AngularJS -管理$http成功回调
- $http成功范围完成加载
- Angularjs在$http成功后运行指令