Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the json data in ejs template?

I have a json data in a controller file and I got this data from an API so that I can be able to redirect this data gotten from the API to an ejs file but when I write the code in the body tag part like this <%-JSON.stringify(jsonData)%>, it will display the entire JSON but when I am using this statement under script tags I don't get anything but [object object] error message is what comes out.

How can I use this JSON data inside script tags for displaying each and every key/value pair from the JSON data? Can you anyone suggest an answer?

In Controller:

res.render('display', {
  jsonData: storeJSONData
}); 

I wrote this code for a redirecting template.

like image 282
Nag Avatar asked Aug 08 '15 04:08

Nag


1 Answers

No talk just watch :P

I have send data like this.

res.render('studentlist',{'studentlist' : result} );

Then I show those data in a table like this baby :D

<table class="table table-inverse">
  <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
      <th>Username</th>
    </tr>
  </thead>

  <tbody>
    <% for(var i=0; i < studentlist.length; i++) { %>
    <tr>
      <td><%= studentlist[i].name %></td>
      <td><%= studentlist[i].email %></td>
      <td><%= studentlist[i].username %></td>
    </tr>
    <% } %>
  </tbody>
</table>

I hope it will help you out there. I am pretty sure . :) Thank you very much.

like image 138
Md. Alif Al Amin Avatar answered Oct 21 '22 01:10

Md. Alif Al Amin