在此解决方案的基础上构建 here ,我正在努力能够接收来自 Node 服务器的响应。
Angular Controller
$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 路由上,通常您会从 Controller 发送数据,以可能查询数据库并通过 JSON 发回一些数据。
关于javascript - Angular Controller 向 Nodejs 服务器发送数据或从 Nodejs 服务器接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43430475/