将对象从EJS传递到JavaScript函数
我正在尝试从我的EJS文件附加一个点击事件,但是我注意到,当我的数据来自数据库具有“ newline”时,我会收到一个错误,上面说:在JSON中的json中的意外令牌253
app.js
app.get('/', async (req, res) => {
return res.render('page', {
data: dataArray
})
});
page.ejs:
<% for(let t of data) { %>
<tr class="tr-<%= t.id %>">
<td><i class="fas fa-pencil-alt" onclick="editT('<%= JSON.stringify(t) %>')"></i>
</td>
<td><%= t.name %></td>
<td><%= t.description %></td>
</tr>
<% } %>
脚本
function editT(t) {
t = JSON.parse(t);
console.log(t)
}
描述具有“ newline”时,我使用json.stringif(t)
它被打破了:
let t = '{"id":1,"name":"Lorem","description":"Lorem Ipsum is simply dummy text of the printing and typesetting industry!
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry.
","created_at":"2022-04-18T22:19:00.153Z"}';
console.log(JSON.parse(treatment));
I'm trying to attach a click event from my ejs file but I noticed that when my data coming from my database has a "newline", I get an error that says: Unexpected token in JSON at position 253
app.js
app.get('/', async (req, res) => {
return res.render('page', {
data: dataArray
})
});
page.ejs:
<% for(let t of data) { %>
<tr class="tr-<%= t.id %>">
<td><i class="fas fa-pencil-alt" onclick="editT('<%= JSON.stringify(t) %>')"></i>
</td>
<td><%= t.name %></td>
<td><%= t.description %></td>
</tr>
<% } %>
script
function editT(t) {
t = JSON.parse(t);
console.log(t)
}
When a description has a "newline" and I use JSON.stringif(t)
it comes broken:
let t = '{"id":1,"name":"Lorem","description":"Lorem Ipsum is simply dummy text of the printing and typesetting industry!
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text of the printing and typesetting industry.
","created_at":"2022-04-18T22:19:00.153Z"}';
console.log(JSON.parse(treatment));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种做到这一点的方法毫无疑问
方式编写UNESCAPER的字符串
,首先使用
&lt;% - %&gt;
这样,它将以这样的:第二个将使用Scode Sectode
第二个也需要您执行
there two way of doing this without error
first one using
<%- %>
this way it will write the unescaped stringlike this :
second one would be using encode
the second one also requires you to do