javascript 如何在ejs模板中显示json数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31889807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:31:29  来源:igfitidea点击:

How to display the json data in ejs template?

javascriptnode.jsejs

提问by Nag

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.

我在控制器文件中有一个 json 数据,我从 API 中获取了这些数据,以便我能够将从 API 中获取的这些数据重定向到 ejs 文件,但是当我在 body 标记部分中编写代码时,像这样 <% -JSON.stringify(jsonData)%>,它将显示整个 JSON,但是当我在脚本标签下使用此语句时,我什么也没有得到,但出现了 [object object] 错误消息。

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?

如何在脚本标签中使用此 JSON 数据来显示来自 JSON 数据的每个键/值对?你能建议一个答案吗?

In Controller:

在控制器中:

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

I wrote this code for a redirecting template.

我为重定向模板编写了此代码。

回答by Md. Alif Al Amin

No talk just watch :P

不说话只看:P

I have send data like this.

我已经发送了这样的数据。

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

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

然后我在像这个宝贝一样的​​表格中显示这些数据: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.

我希望它能帮助你。我很确定 。:) 非常感谢你。

回答by Inamur Rahman

The following is the 'docs' that we pass from the controller to view.

以下是我们从控制器传递给视图的“文档”。

app.get('/', function(req, res){
    User.find((err, docs) =>{
    if(!err)
      res.render('home', {'docs':docs});    
    else
      console.log(JSON.stringify(err));
    });
});

And now we can use the variable docs to display on ejs templeate. Example

现在我们可以使用变量 docs 在 ejs 模板上显示。例子

<ul class="collection">
 <% datas.forEach(function(data) { %>
    <li class="collection-item avatar">
      <span class="title"><%= data.name %></span>
      <p>First Line <br>
         <%= data.email %>
      </p>
    </li>
  <% }); %>
  </ul>

I hope it will work.

我希望它会起作用。

回答by michelem

Please try this in your HTML and it should work:

请在您的 HTML 中尝试此操作,它应该可以工作:

<script type="text/javascript">
    function codeAddress(){ 
         var data = <%- jsonData %>;
         document.write(JSON.stringify(data)); 
    }
</script>

This is because you can't use JS inside the EJS tags, you need to render it to be assigned to a varand then you can play with it.

这是因为你不能在 EJS 标签内使用 JS,你需要渲染它以分配给 avar然后你可以玩它。

回答by mscdex

Try changing your render()to:

尝试将您的更改render()为:

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

and your template to:

和您的模板:

<script type="text/javascript">
function codeAddress() {
  var data = <%- jsonData %>;
  document.write(data);
}
</script>