0
開始學習Node.js的,發送POST
要求用Node.js的:在響應正文與Node.js請求之間獲取undefined?
var http = require('http')
, https = require('https')
, _ = require('underscore')
, querystring = require('querystring');
// Client constructor ...
Client.prototype.request = function (options) {
_.extend(options, {
hostname: Client.API_ENDPOINT,
path: Client.API_PATH,
headers: {
'user-agent': this.agent
}
});
var req = (this.secure ? https : http).request(options);
if(options.data) req.write(querystring.stringify(options.data));
req.end();
req.on('response', function (res) {
res.on('data', function (chunk) {
res.body += chunk;
});
res.on('end', function() {
console.log(res.body);
});
});
}
體顯示:undefined<xml version="1.0" encoding="UTF-8">
。
undefined
從哪裏來?
我有多愚蠢?謝謝... – gremo 2013-02-13 16:05:46
如果你打算把'chunk'當成一個字符串,那麼你應該添加'res.setEncoding('utf8')'。您當前的代碼可能會失敗多字節字符。 – loganfsmyth 2013-02-13 16:51:10