问题是params是在函数范围内定义的,所以当我尝试在函数外部发出请求时,它会显示未解析的变量或类型参数
我有什么方法可以改写这个。谢谢你的时间
var https = require('https');
var querystring = require('querystring');
function test(access_token)
{
// setup our request parameters
var myreq = {
'location' : '1200, NewYork, USA',
};
// stringify our request parameters
var myreqstr = querystring.stringify(myreq);
// setup params for the request
var params = {
host: 'api.yelp.com',
port: '443',
path: '/v3/businesses/search?' + myreqstr, // include the URL parameters
method: 'get',
headers : {
'Authorization' : 'Bearer ' + access_token,
}
};
// call back function will show the results
callback = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});
};
// make the request
var req = https.request(params, callback).end();
}
令牌由此回调函数
发送callback = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
var tokens = JSON.parse(str);
test(tokens.access_token);
});
};