在Node.js中编写格式化的JSON
||
我正在使用Node.js将JSON POST到PostBin,但是数据格式错误(如您在此处看到:http://www.postbin.org/1cpndqw)。
这是我用于tesT的代码:
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();
没有找到相关结果
已邀请:
4 个回复
寇剩
师埠女
,其中
是要用作缩进单位的空格数。如果需要制表符,也可以使用
。这实际上是ECMAScript 5规范的一部分,并记录在MDN中。
贸会
容淑阔九