【问题标题】:Angular Controller Sending and Receiving Data to/from a Nodejs ServerAngular 控制器向/从 Nodejs 服务器发送和接收数据
【发布时间】:2017-04-15 19:39:04
【问题描述】:

在此解决方案here 的基础上,我正在努力接收来自节点服务器的响应。

角度控制器

$scope.loginUser = function() {
      $scope.statusMsg = 'Sending data to server...';
      $http({
             url: 'http://##.##.##.##/login', 
             method: 'POST',
             data: user,
             headers: {'Content-Type': 'application/json'}
      })
}

Nodejs 服务器

app.post('/login', function(req, res) {
   console.log('Processing request...');
   res.sendFile('/success.html');
});

如何扩展此示例以接收来自服务器的响应?

【问题讨论】:

    标签: javascript angularjs node.js express


    【解决方案1】:
    $scope.loginUser = function() {
      $scope.statusMsg = 'Sending data to server...';
      $http({
             url: 'http://##.##.##.##/login', 
             method: 'POST',
             data: user,
             headers: {'Content-Type': 'application/json'}
      }).then(function(response) {
          //do something with your response from the server
          console.log(response.data)
        })
     }
    

    在您的 /Post 路由上,您通常会从控制器发送数据以查询数据库并通过 JSON 发回一些数据。

    【讨论】:

      猜你喜欢
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 2016-10-19
      • 1970-01-01
      相关资源
      最近更新 更多