0

我有以下内容:

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 键/值对。我刚刚收到原始对象。我哪里错了?

4

1 回答 1

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);
    }
  });
});
于 2013-03-15T12:39:28.170 回答