【问题标题】:$http request on node js - how to return a value in success $http.post节点 js 上的 $http 请求 - 如何成功返回值 $http.post
【发布时间】:2016-09-03 21:29:42
【问题描述】:

我正在编写 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);
});  
}

我希望我的 login.js 文件中的 userLogin 函数具有 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 文件中的调用方法..

你知道怎么做吗?

谢谢

【问题讨论】:

    标签: javascript angularjs node.js http


    【解决方案1】:

    返回一个承诺而不是隐藏它

    $scope.userLogin = function (info) {
        return $http.post('/authenticate_user', info);
    }
    

    用法

    userLogin({name: '', password: ''}).then(function(success) {
      console.log(success)
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 2016-03-14
      • 1970-01-01
      相关资源
      最近更新 更多