2013-04-17 50 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); 
});