如何在javascript中从回调函数中获取整个数据
我编写了以下函数,该函数从url获取json数据。
function getWeatherDataForCities(cityArray){ var arrAllrecords = []; var toDaysTimestamp = Math.round((new Date()).getTime() / 1000) - (24*60*60); for(var i in cityArray){ for(var j=1; j<=2; j++){ var jsonurl = "http://api.openweathermap.org/data/2.5/history/city?q="+cityArray[i]+"&dt="+toDaysTimestamp; $.ajax({ url: jsonurl, dataType: "jsonp", mimeType: "textPlain", crossDomain: true, contentType: "application/json; charset=utf-8", success: function(data){ arrAllrecords[j]["cityName"] = data.list[0].city.name; arrAllrecords[j]["weather"] = data.list[0].weather[0].description; } }); toDaysTimestamp = toDaysTimestamp - (24*60*60); } } return arrAllrecords; //returning arrAllrecords } $(document ).ready(function() { var cityArray = new Array(); cityArray[0] = "pune"; var arrAllRecords = getWeatherDataForCities(cityArray); //Print All records returned by getWeatherDataForCities(cityArray); });
我在上面的代码中写了一些注释。我调用了getWeatherDataForCities
function
,它返回url
所有记录。我在函数中声明了getWeatherDataForCities
array
。我想在该array
中添加所有返回的记录。我已经尝试过如上所述但是没有任何进展array
。
在控制台中显示j和arrAllrecords未定义。
如何从回调函数中获取数组中的所有记录?
您的回复将受到高度赞赏
你的getWeatherDataForCities
函数不会返回任何内容,因为ajax操作是异步的。 你需要使用回调。
修改您的函数以接受回调函数:
function getWeatherDataForCities(cityArray, callback){ var arrAllrecords = []; var toDaysTimestamp = Math.round((new Date()).getTime() / 1000) - (24*60*60); for(var i in cityArray){ for(var j=1; j<=2; j++){ var jsonurl = "http://api.openweathermap.org/data/2.5/history/city?q="+cityArray[i]+"&dt="+toDaysTimestamp; $.ajax({ url: jsonurl, dataType: "jsonp", mimeType: "textPlain", crossDomain: true, contentType: "application/json; charset=utf-8", success: function(data){ arrAllrecords[j]["cityName"] = data.list[0].city.name; arrAllrecords[j]["weather"] = data.list[0].weather[0].description; // call the callback here callback(arrAllrecords); } }); toDaysTimestamp = toDaysTimestamp - (24*60*60); } } }
并像这样使用它:
$(document ).ready(function() { var cityArray = new Array(); cityArray[0] = "pune"; getWeatherDataForCities(cityArray, function(arrAllrecords) { // Do something with your data }); });
您正在尝试使用空数组。 获取值时,它将始终返回undefined。
var arrAllrecords = []; arrAllrecords[2]; //undefined arrAllrecords[2]["cityname"]; //undefined
你应该更好地使用对象数组。
我不知道你为什么使用变量j。 以下代码适用于我。
var arrAllrecords = []; function getWeatherDataForCities(cityArray){ var toDaysTimestamp = Math.round((new Date()).getTime() / 1000) - (24*60*60); for(var i in cityArray){ var jsonurl = "http://api.openweathermap.org/data/2.5/history/city?q="+cityArray[i]+"&dt="+toDaysTimestamp; $.ajax({ url: jsonurl, dataType: "jsonp", mimeType: "textPlain", crossDomain: true, contentType: "application/json; charset=utf-8", success: function(data){ arrAllrecords.push({ "cityName" : data.list[0].city.name, "weather" : data.list[0].weather[0].description }); if(arrAllrecords.length === cityArray.length) { callback(arrAllrecords); } } }); } } function callback(arrAllrecords) { console.log(arrAllrecords); } $(document).ready(function() { var cityArray = new Array(); cityArray[0] = "pune"; cityArray[1] = "mumbai"; cityArray[2] = "delhi"; getWeatherDataForCities(cityArray); });