我想从Ionic向节点服务器发送数据。然后用节点更改数据并将其他数据返回给Ionic。从节点服务器返回数据到Ionic(Angularjs)客户端
但我现在不能如何将数据返回到离子。使用离子(Angularjs)
客户:从服务器
$http({
method: 'POST',
url: 'http://127.0.0.1:3000',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: JSON.stringify("Test")
});
服务器节点
http = require('http');
server = http.createServer(function(req, res) {
if (req.method == 'POST') {
console.log("POST");
var body = '';
req.on('data', function (data) {
body += data;
console.log("Partial body: " + body);
});
req.on('end', function() {
console.log("Body: " + body);
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('success');
res.end('success');
}else{
console.log("GET");
res.writeHead(200, {'Content-Type': 'text/html'});
res.end("success");
}
});
port = 3000;
host = '127.0.0.1';
server.listen(port, host);
console.log('Listening at http://' + host + ':' + port);
日志输出
[email protected]:~/svn/nodejs$ node server.js
Listening at http://127.0.0.1:3000
POST
Partial body: "Test"
Body: "Test"
在客户端代码中,http函数必须采用一个'回调函数',它会随服务器的响应一起提供。 –