0

I am making an AJAX get request that displays JSON data in a HTML table. I know how to do this with Javascript, so I thought I'd try it with jQuery. There is a problem in my .each loop. I am vague on how the arguments (key, object) process more than one key value pair in each position and suspect this is where my error lies.

I have tried JSON.parse, but that didn't help. I am definitely getting the data as I can display it in an alert box. I suspect that what I'm doing is not industry standard and that there is a more elegant way to reach my objective.

$("#button").click(function () {
    $.ajax({
        type: 'get',
        url: "http://www.adweb.agency/interview/api/animals",
        data: {format: 'json'},
        dataType: 'json',
        success: function (data) {
            var i = 0;
            var table = '<table class="mainTable"><tr><th>item</th><th>image</th><th>description</th></tr>';

            $.each(data, function (key, object) {
                table += ('<tr>');
                table += ('<td>' + data.Title + '</td>');
                table += ('<td><img src="' + data.ImageURLs.Thumb + '"></td>');
                table += ('<td>' + data.Description + '</td>');
                table += ('</tr>');
            });

            table += '</table>';

            $('#tableContainer').html(table);
        }
    });
});
2
  • What error are you getting? And also if you can provide the JSON you're getting. Commented Jun 30, 2016 at 15:39
  • the json is coming from adweb.agency/interview/api/animals . I have no error message. Commented Jun 30, 2016 at 15:42

3 Answers 3

1

Here's the right code that you need, just copy and paste, regards!

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>

<input id="btnSubmit" type="button" value="Release"/>

<div id="tableContainer">
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnSubmit").click(function () {
            $.ajax({
                dataType: 'json',
                url: "http://www.adweb.agency/interview/api/animals",
                type: 'get',
                success: function (data) {
                    var i = 0;
                    var table = '<table class="mainTable"><tr><th>item</th><th>image</th><th>description</th></tr>';
                    data = $.parseJSON(data)
                    $.each(data, function (idx, obj) {
                        table += ('<tr>');
                        table += ('<td>' + obj.Title + '</td>');
                        table += ('<td><img src="' + obj.ImageURLs.Thumb + '"></td>');
                        table += ('<td>' + obj.Description + '</td>');
                        table += ('</tr>');
                    });
                    table += '</table>';
                    $("#tableContainer").html(table);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("some error");
                }
            });
        });
    });
</script>
</body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that your response variable 'data' is an array, you're accessing the wrong variable inside each iteration of your each loop:

$("#button").click(function(){    

    $.ajax({
        type: 'get',
        url: "http://www.adweb.agency/interview/api/animals",
        data: {format: 'json'  },
        dataType: 'json',
        success: function(data){  

            var i = 0;
            var table = '<table class="mainTable"><tr><th>item</th><th>image</th><th>description</th></tr>';

            // NOTE!  I changed 'object' to 'value' here
            // NOTE 2!  We added a JSON.parse on the 'data' variable to convert from JSON to JavaScript objects.
            $.each(JSON.parse(data), function(key, value){ 

                // We need to access the value variable in this loop because 'data' is the original array that we were iterating!
                table += ('<tr>');
                table += ('<td>' + value.Title + '</td>');
                table += ('<td><img src="' + value.ImageURLs.Thumb + '"></td>');
                table += ('<td>' + value.Description + '</td>');
                table += ('</tr>');

            });

            table += '</table>'; 

            $('#tableContainer').html(table);

        }

    });

});

See the comments in the above code for the changes.

See the docs on jQuery's .each() iterator here.

3 Comments

Sorry. This did not work. It does help me understand this better.
Check the new code. Your original idea to JSON.parse was correct, but only with the newly changed variables within the .each() loop that I provided. Give the new code a shot. In the future, when you are asking for help in parsing responses via AJAX or the like, it's a good idea to post an example of what the response from the API looks like. In this case, I had to do a request myself to see the result.
Thank you. I'm here to learn and all tips appreciated. I will as you advise,
-1

Try this code:

$.each(data, function(key, object){ 

    table += '<tr>';
    table += '<td>' + data[key].Title + '</td>';
    table += '<td><img src="' + data[key].ImageURLs.Thumb + '"></td>';
    table += '<td>' + data[key].Description + '</td>';
    table += '</tr>';
});

3 Comments

I think you'll need to give more details about what you are getting
i'm not getting much. When I click the button I get the table header but no data in the table.
If you console.log(table) what is the result?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.