簡體   English   中英

Angular Controller向Nodejs服務器發送數據或從Nodejs服務器接收數據

[英]Angular Controller Sending and Receiving Data to/from a Nodejs Server

建立在這個解決方案在這里 ,我的工作能夠接收響應從節點服務器返回。

角度控制器

$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');
});

如何擴展此示例以使其能夠接收來自服務器的響應?

$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發送回一些數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM