我正在将EJS与Node.JS一起使用,并将
JSON对象传递给它,但需要将其作为页面中的可用对象进行访问.我从这里得到了意外的令牌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,"tags":["indian","groceries","ethnic","store"],"_id":"51c0519e02b7cbec73000002"}]);
解决方法
试试这个:
var initData = JSON.parse('<%-JSON.stringify(list)%>');
要么:
var initData = <%-JSON.stringify(list)%>;