-1

I am trying to populate an html table of JSON data. I was provided the JSON data and cannot determine why it isn't populating but suspect the JSON may not be formatted properly. When I use other sample data it works, then when I sub in the JSON I was provided it doesn't work. I've tried copying the JSON into a file on my direct server, linking to what I was provided (here: https://boiling-fortress-75456.herokuapp.com/) and inserting it on myjson.com and reformatting the JSON data.

Here is my code:

<script>
    $(function() {

      var entries = [];
      var dmJSON = "https://api.myjson.com/bins/6sjud?callback=?";
      $.getJSON(dmJSON, function(data) {
        $.each(data.entries, function(i, f) {
          var tblRow = "<tr>" + "<td>" + f.rank + "</td>" + "<td>" + f.name + "</td>" + "<td>" + f.march_rank + "</td>" + "<td> " + f.april_rank + "</td>" + "<td>" + f.may_rank + "</td>" + f.personal_volume + "</td>" + f.team_volume + "</td>" + "</tr>"
          $(tblRow).appendTo("#incentive tbody");
        });
      });
    });
    </script>


    <div class="wrapper">
      <div class="profile">
        <table id= "incentive" border="1">
          <thead>
            <th>Rank</th>
            <th>Name</th>
            <th>March</th>
            <th>April</th>
            <th>May</th>
            <th>Highest Rank</th>
            <th>Personal Volume</th>
            <th>Team Volume</th>
          </thead>
        <tbody>

     </tbody>
  </table>

  </div>
 </div>
3
  • 4
    What is your question? Commented Mar 1, 2018 at 14:54
  • The JSON coming back from that URL in your code has no property entries. Commented Mar 1, 2018 at 14:55
  • Note: then when I sub in the JSON I was provided it doesn't work. "Doesn't work" isn't a very useful description on the problem. What happens? Do you get an error message? What error message? Does it just not do what you expected? What did you expect and how does what actually happens differ. Commented Mar 1, 2018 at 15:11

1 Answer 1

0

You need to get the objects from affiliate

$.each(data.affiliate, function(i, f) {
    var tblRow = "<tr>" + "<td>" + f.rank + "</td>" + "<td>" + f.name + "</td>" + "<td>" + f.march_rank + "</td>" + "<td> " + f.april_rank + "</td>" +  "<td>" + f.may_rank + "</td>" + f.personal_volume + "</td>" + f.team_volume + "</td>" + "</tr>"
    $(tblRow).appendTo("#incentive tbody");
});

Response from service

{
    "affiliate":[{          
            "rank":1,"name":"Sally","march_rank":"Gold","april_rank":"Silver","may_rank":"Silver","highest_rank":"Silver","team_volume":12345
        },{
            "rank":2,"name":"Zelda","march_rank":"Gold","april_rank":"Bronze","may_rank":"Silver","highest_rank":"Gold","team_volume":12345
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

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.