我有以下内容:
var myObject={name:"name"};
var options={
hostname:'www.google.com',
port:80,
method:'GET'
};
var req=http.request(options,function(res){
res.on('data', function (chunk) {
myObject.serverResponse=chunk;
console.log(myObject);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
在控制台中我没有获得serverResponse键/值对。我刚刚收到原始对象。我哪里出错?
答案 0 :(得分:0)
var req = http.request(options, function (res) {
var serverResponse = '';
res.on('data', function (chunk) {
serverResponse += chunk;
});
res.on('end', function () {
try {
myObject.serverResponse = JSON.parse(serverResponse);
console.log(myObject);
} catch (err) {
console.log(err);
}
});
});