0

I have a JSON file named detail.json and it is like this

{"name":"bhanu","constituency":"vizag","party":"bjp"},
{"name":"nithya","constituency":"godavari","party":"bjp"}

I want to display this as a HTML table ans I've written this

<table id="deets">
      <tr>
        <th>Name</th>
        <th>const</th>
        <th>party</th>
      </tr>
    </table>
<script type="text/javascript">
  $(document).ready(function(){
    $.getJSON("detail.json",function(data){
        var det = '';
        $.each(data,function(key,value){
           det +='<tr>';
           det +='<td>'+value.name+'</td>';
           det +='<td>'+value.constituency+'</td>';
           det +='<td>'+value.party+'</td>';
           det += '</tr>';

        });
        $('#deets').append(det); 
    });
  });

</script>

But when I open it only the table header is being displayed but the data is not being parsed from the JSON file. What's the mistake in this code and what could be the issue? I've tried it many times.

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
nidhi_1729
  • 13
  • 2
  • 1
    The code itself looks kinda ok, so - are there any error messages in the console? Can you add some `console.log()` statements to see how the code executes? – Vilx- May 02 '21 at 12:07
  • https://stackoverflow.com/questions/14687273/not-able-to-fetch-the-json-file-in-google-chrome – lmngz May 02 '21 at 12:07
  • Did you ensure whether the code in AJAX call is executing or not? – T J May 02 '21 at 12:32
  • @Vilx- I added square brackets in the JSON file but the problem now is that if I add data to the json file using a html form, the data is getting added outside the brackets. What can I do about it? – nidhi_1729 May 02 '21 at 12:36
  • 1
    @nidhi_1729 please [edit] and update the question with new problem, or create a new question, including details of code for that you use for "add data to the json file using a html form" – T J May 02 '21 at 12:44

0 Answers0