I have two $.getJSON methods with in a function in my Javascript file, but the first of the two methods does not seem to return any data on occasions. I can not seem to find a pattern or why this is. The second method always runs though. Does anybody know why this is? Below is my code:-
$(document).ready(function(){
$(document).on('click', '.read-one-recipe-button', function(){
var id = $(this).attr('data-id');
var read_ingredients_html = "";
$.getJSON("http://localhost:8080/recipe_app/api/recipes/read_ingredients.php?id=" + id, function(data_ingredients){
read_ingredients_html+="<ul>";
$.each(data_ingredients.records, function(key, val){
read_ingredients_html+="<li>";
read_ingredients_html+=val.Ingredient + " " + val.Quantity + " " + val.Comments;
read_ingredients_html+="</li>";
});
read_ingredients_html+="</ul>";
});
$.getJSON("http://localhost:8080/recipe_app/api/recipes/read_one_recipe.php?id=" + id, function(data_recipe){
var read_one_recipe_html ="";
read_one_recipe_html+="<div id='read-recipes' class='btn btn-primary pull-right m-b-15px read-recipes-button'>";
read_one_recipe_html+="<span class='glyphicon glyphicon-list'></span> Read Recipes";
read_one_recipe_html+="</div>";
read_one_recipe_html+="<table class='table table-bordered table-hover'>";
read_one_recipe_html+="<tr>";
read_one_recipe_html+="<th>Recipe</th>";
read_one_recipe_html+="<th>Ingredients</th>";
read_one_recipe_html+="<th>Step One</th>";
read_one_recipe_html+="<th>Step Two</th>";
read_one_recipe_html+="<th>Step Three</th>";
read_one_recipe_html+="<th>Step Four</th>";
read_one_recipe_html+="</tr>";
read_one_recipe_html+="<tr>";
// Recipe Name
read_one_recipe_html+="<td class='w-15-pct'>" + data_recipe.recipe_name + "</td>";
// Recipe Ingredients
read_one_recipe_html+="<td class='w-20-pct'>" + read_ingredients_html + "</td>";
// Recipe Step One
read_one_recipe_html+="<td class='w-20-pct'>" + data_recipe.instruction_one + "</td>";
// Recipe Step Two
read_one_recipe_html+="<td class='w-20-pct'>" + data_recipe.instruction_two + "</td>";
// Recipe Step Three
read_one_recipe_html+="<td class='w-20-pct'>" + data_recipe.instruction_three + "</td>";
// Recipe Step Four
read_one_recipe_html+="<td class='w-20-pct'>" + data_recipe.instruction_four + "</td>";
read_one_recipe_html+="</tr>";
read_one_recipe_html+="</table>";
$('#page-content').html(read_one_recipe_html);
changePageTitle("Read Recipe");
});
});
});