Razvan Zamfir Razvan Zamfir - 9 months ago 114
Ajax Question

jQuery JSON parser: "Unexpected identifier" error

I have made a small (test) script that parses a JSON file (I use https://jsonplaceholder.typicode.com in the example below) and makes table rows out of the resulting object.

I am trying to append these rows to a table.

See the jsFiddle HERE.

I get an

Unexpected identifier
error that I find inexplicable. What am I doing wrong?

UPDATE: See my updated fiddle HERE. Thanks to all that helped.

Answer Source

You were appended the data wrong way. Replace your this portion of code by this.

var root = 'https://jsonplaceholder.typicode.com';
var tableRow = '';
$.ajax({
  url: root + '/users',
  method: 'GET',
  success: function(data) {
    for (var i = 0; i < data.length; i++) {
      var tableRow = '<tr><td>' + data[i].name + '</td><td>' + data[i].email + '</td><td>' + data[i].email + '</td></tr>';
      $("#dataTable").find('tbody').append(tableRow);
    }
  },
  error: function() {
    var tableRow = '<tr><td>There is no data to display</td></tr>';
    console.log(tableRow);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table" id="dataTable">
  <thead>
    <tr>
      <th>Full Name</th>
      <th>Email</th>
      <th>Address</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download