0

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");
        });
    });   

});
tech2017
  • 1,806
  • 1
  • 13
  • 15
ED209
  • 588
  • 1
  • 9
  • 26
  • Could be a number of different things. I actually suspect there is something wrong with the API rather than the piece of code you posted. You should monitor the Network tab in your console and see if the calls to `http://localhost:8080/recipe_app/api/recips/...` are also successfull when no data is returned (i.e. HttpStatus 200 = ok, 500 = server error, etc) – nbokmans Jun 06 '17 at 14:27
  • This is not a duplicate of the linked question in my opinion. – nbokmans Jun 06 '17 at 14:28
  • Hi nbokmans. Thanks for your reply. I have monitored the network tab in chromes developer console, I called the function 20 times and the status of both calls were always successful (i.e. HttpStatus 200). – ED209 Jun 06 '17 at 14:38

0 Answers0