jquery - JavaScript variable is "null" -
on line 30 facing issue $.each(data.menu, function (). being told console "data null". can explain whats going on? thanks
function getfoodmenudata () { var url = 'http://localhost:8888/tom_carp_final_project/chorizios/foodmenu.json'; $.getjson(url, function (data) { window.localstorage.setitem('choriziosmenu333', json.stringify(data)); }); } function showfoodmenudata () { var data = json.parse(window.localstorage.getitem('choriziosmenu333')); var images = ""; $.each(data.menu, function () { images += '<li class="list-group-item"><img style="width: 100%;" src= "' + this.url + '"></li>'; images += '<li class="list-group-item">' + this.description + '</li>'; }); $('#foodmenu').append(images); } showfoodmenudata();
you have call getfoodmenudata()
, , inside callback asynchronous $.getjson
, call showfoodmenudata()
.
function getfoodmenudata() { var url = 'http://localhost:8888/tom_carp_final_project/chorizios/foodmenu.json'; $.getjson(url, function(data) { window.localstorage.setitem('choriziosmenu333', json.stringify(data)); showfoodmenudata(); // <--- call inside callback }); } function showfoodmenudata() { var data = json.parse(window.localstorage.getitem('choriziosmenu333')); var images = ""; $.each(data.menu, function() { images += '<li class="list-group-item"><img style="width: 100%;" src= "' + this.url + '"></li>'; images += '<li class="list-group-item">' + this.description + '</li>'; }); $('#foodmenu').append(images); } getfoodmenudata(); // <--- call first
Comments
Post a Comment