我希望将一个变量绑定到我的请求对象中,这样当回调被执行时,我就可以访问这个变量。
这是相关的库: https://github.com/request/request 这是我的代码。
这是相关的库: https://github.com/request/request 这是我的代码。
var request = require('request');
for (i = 0; i < cars.length; i++) {
request({
headers: { 'Content-Type': 'application/json'},
uri: 'https://example.com',
method: 'POST',
body: '{"clientId": "x", "clientSecret": "y"}'
},
function(err, res, body){
// I want to put the correct i here.
// This outputs cars.length almost everytime.
console.log(i);
});
}
JSON.stringify({"clientId": "x", "clientSecret": "y"})
吗? - mugabits