在函数getweather()中,我确实从 Json 对象中获取了一些天气数据,并将该数据存储在变量data中。在该函数中,我可以处理 Json 数据,但是如何从getweather()外部访问数据?
如果我返回变量location或data并不重要。变量位置不是 Json。
如何处理place变量以使其像在函数getweather()中一样工作?
var ajax = require('ajax');
var place;
place = getweather();
function getweather()
{
// Make the request
ajax(
{
url: 'http://api.openweathermap.org/data/2.5/weather?q=paris.fr', type: 'json'
},
function(data)
{
// Success!
console.log("Successfully fetched weather data!");
// Extract data
var location = data.name;
return location;
},
function(error) {
// Failure!
console.log('Failed fetching weather data: ' + error);
}
);
}