我将EJS与Node.JS结合使用,并向其中传递JSON对象,但需要访问它作为页面中的可用对象。我从这得到了意外的 token o错误:

var initData=JSON.parse(<%-JSON.stringify(list)%>);

我无法弄清楚这里出了什么问题。

这是呈现时在文件中的样子:
var initData=JSON.parse([{"title":"South Hills Health System - University Health Center","adr":"200 Lothrop St,15213","coords":"40.441875,-79.960813","images":[],"tags":[],"_id":"51c0e9798384f40000000017"},{"title":"Bombay Food Market","adr":"4605 Centre Avenue, Pittsburgh, PA 15213, USA","coords":null,"images":[],"tags":["indian","groceries","ethnic","store"],"_id":"51c0519e02b7cbec73000002"}]);

最佳答案

试试这个:

var initData = JSON.parse('<%-JSON.stringify(list)%>');

或者:
var initData = <%-JSON.stringify(list)%>;

关于javascript - 从EJS到JS中JSON对象的JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17184179/

10-10 00:10
查看更多