2013-04-17 52 views
0

我的代码点击了POST URL并接收到数据。下面是代码:HTTP POST中的处理错误

var postdata="{u:1}"; 
var options = { 
       host: 10.0.0.1, 
       port: 80, 
       path: /ddd, 
       method: post 
      }; 
var reqClient = http.request(options, function(res) 
      { 

        var data = ''; 
        res.setEncoding('utf8'); 
      }); 
//setting the request contenet type to application/json 
      reqClient.setHeader('Content-Type','application/json'); 
      //Posts the data to the Server 
      reqClient.write(postData); 
      reqClient.end(); 
reqClient.on('response', function (res) 
       { 
         var data = ''; 
         res.on('data', function (chunk) 
         { 
          data+=chunk;        
         }); 
res.on('end',function(){ 
//Perform some functions 
}); 
}).on('error', function(){ 
         //error handler 
        }); ; 

我要处理一个错误,当服务器无法打到客户端URL说。我可以按照上面的方法吗?我被困在这里。

回答

1

您可能希望为在Node.js HTTP Documentation上详述的错误事件添加另一个事件侦听器。

req.on('error', function(e) { 
    console.log('problem with request: ' + e.message); 
});