Tôi đang sử dụng Node.js để POST JSON lên PostBin nhưng dữ liệu bị định dạng sai (như bạn có thể thấy ở đây: http://www.postbin.org/1cpndqw).Viết JSON được định dạng trong Node.js
Đây là mã tôi đang sử dụng để kiểm tra:
var http = require('http');
var options = {
host: 'www.postbin.org',
port: 80,
path: '/1cpndqw',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.write(JSON.stringify({ a:1, b:2, c:3 }, null, 4));
req.end();
'{ "a": 1, "b": 2, "c": 3} 'nào bạn sửa chữa vấn đề của bạn? Có vẻ như bạn đã nhận dữ liệu được đăng chính xác. – Raynos
Dữ liệu là chính xác nhưng "xấu xí", tôi muốn gửi như thế này, dễ hiểu hơn: http://www.postbin.org/1ijyltn#xa6rim – donald