【问题标题】:node express and angular post action节点快递和角度发布动作
【发布时间】:2017-06-23 12:25:18
【问题描述】:

简介

我有一个角度帖子,发布到我的节点快递后端,将数据发送到 API,如果帖子成功,我希望将用户发送到不同的角度页面,如果不正确则显示错误消息。

我在想如果成功将变量发布到我的角度,我可以在我的范围内使用 ng-hide/show 或加载新页面(如果存在变量)。

但也许有更好的方法来做到这一点?

我的角帖

FirstModule.controller('LoginController', function ($scope, $http) {
    $scope.formData = {};

    $scope.LoginForm = function () {
        var data = {
            LoginEmail: $scope.formData.LoginEmail
        };

    $http({
        url: 'http://localhost:8080/back-end/test',
        method: "POST",
        data: data,
        headers: {'Content-Type': 'application/json'}
    }).success(function (data) {
        $scope.formData = data; // assign  $scope.persons here as promise is resolved here
    }).error(function (data, status) {
        $scope.formData = status;
    });
}
});

我的节点快递到A API

这篇文章使用从我的角度发送的电子邮件并将其发布到第三方 API,我知道希望采取行动

  • 在传递时将帖子发布到我的前面,以便我可以使用角度范围内的变量来隐藏或显示新页面
  • 更好的解决方案是我可以通过节点加载新的角度页面

{
            var headers = {
                'User-Agent': 'Super Agent/0.0.1',
                'Content-Type': 'application/x-www-form-urlencoded'
            };
// Configure the request
            var api = result[1].data_api;
            var login_email = result[0].data_login_email;
            var options = {
                url: 'https://pi.pardot.com/api/prospect/version/4/do/read',
                method: 'POST',
                headers: headers,
                form: {
                    'email': login_email,
                    'user_key': userkey,
                    'api_key': api
                },
                json: true // Automatically stringifies the body to JSON
            };

// Start the request
            rp(options)
                .then(function (parsedBody) {
                    console.error(login_email, "Is a user, login pass!");
                    // $scope.FormLogin = true;
                })
                .catch(function (err) {
                    console.error("fail no such user");
                });
            console.error("Third done");
        }

【问题讨论】:

    标签: javascript angularjs node.js express


    【解决方案1】:

    更好的方法是从 API 发送响应代码来告诉 Angular 请求是否成功。

    如果你使用快递,你可以这样做:

    if (everythingOk) {
      res.status(200).send(successData)
    } else {
      res.status(400).send()
    }
    

    在你的角度请求中:

            rp(options)
                    .then(function (parsedBody) {
                        if(parsedBody.statusCode == 200) {
                          console.error(login_email, "Is a user, login pass!");
                        } else {
                          console.error("fail no such user"); 
                          // $scope.FormLogin = true;
                        }
                    })
                    .catch(function (err) {
                        console.error("unknown error");
                    });
                console.error("Third done");
    

    【讨论】:

    • 很好,刚吃完午饭回来,我试着实施这个并让你知道。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    相关资源
    最近更新 更多