Angular 控制器向 Nodejs 服务器发送和接收数据



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

角度控制器

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

Nodejs Server

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

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

$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 发回一些数据。

最新更新