2015-09-23 82 views
0

當使用Request模塊時,我們如何從響應回調訪問請求參數?從請求模塊中的響應回調訪問請求參數

例如,下面的「狗」(等)值可以通過循環列表傳遞:

var u = require('util'); 
var url = "http://example.com/animals/%s"; 

request.get({uri: u.format(url, "dog")}, function (error, response, body) { 
    if (!error && response.statusCode == 200) { 

     //how could we access the value 'dog' here? 
     //something like this: console.log(uri.params.animal); 
    } 
} 

回答

0

您可以用js的封閉性。

var u = require('util'); 
var url = "http://example.com/animals/%s"; 

var param = "dog"; 
request.get({uri: u.format(url, param)}, function (error, response, body) { 
    if (!error && response.statusCode == 200) { 
     //how could we access the value 'dog' here? 
     console.log(param); 
    } 
} 
+0

謝謝,它的工作原理。 – purga