2013-12-20 20 views
1

嗨,我想写一个节点服务器代码来检索从url http://freegeoip.net/json/14.12.111.113经度和纬度信息我总是得到这个错误。http.request的网址http://freegeoip.net/json/14.12.111.113

Exception: Error: getaddrinfo ENOTFOUND 
Error: getaddrinfo ENOTFOUND 
    at errnoException (dns.js:37:11) 
    at Object.onanswer [as oncomplete] (dns.js:124:16) 

有人可以帮忙请。

这里是代码我使用..

var options = {  
    host: 'http://freegeoip.net/',  
    path: 'json/14.12.111.113', 
    method: 'GET' 
}; 

var req = http.request(options, function(res) {  
    console.log('STATUS: ' + res.statusCode);  
    console.log('HEADERS: ' + JSON.stringify(res.headers));  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) { 
     console.log('BODY: ' + chunk); 
    });  
}); 

// write data to request body 

req.write('data\n'); 
req.write('data\n'); 
req.end(); 
+0

请使用代码块的格式,同时张贴代码.. 。 – javadevg

回答

5

不要添加的“http”和路径的开头“/”

var options = {  
    host: 'freegeoip.net',  
    path: '/json/14.12.111.113', 
    method: 'GET' 
}; 

var req = http.request(options, function(res) {  
    console.log('STATUS: ' + res.statusCode);  
    console.log('HEADERS: ' + JSON.stringify(res.headers));  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) { 
     console.log('BODY: ' + chunk); 
    });  
}); 

// write data to request body 

req.write('data\n'); 
req.write('data\n'); 
req.end(); 
+0

非常感谢!有效 :) – ATHER