I'm building some html with a getJSON call. Inside that call there's another getJSON call. I'm trying to return two variables to use in my script a few lines of code further. The problem is that the script keeps returning undefined
. A console.log tells me that the variables return the correct answer. I do not have any errors whatsoever.
$.getJSON(url, function (data) {
var productsHtml = [];
$.each(data.collection.products, function (index, product) {
var pUrl = product.url;
var productHtml = '' + ....blablabla....
var stock;
var delivery;
$.getJSON(pUrl + '?format=json', function (data) {
if (data.product.stock.level >= 1) {
var stock = '<span class="ov">' + data.product.stock.level + '</span>';
var delivery = data.product.stock.delivery.title;
} else {
var stock = '<span class="nov">' + data.product.stock.level + '</span>';
var delivery = data.product.stock.delivery.title;
}
console.log(stock, delivery); //this returns the correct html
});
var productHtml = productHtml + '<li class="amount">' + stock + '</li><li class="deliverytime">' + delivery + '</li>... etcetc....
Anybody know what I'm doing wrong?
Thx in advance...