简体   繁体   中英

How to pass JSON data from ejs to normal inline javascript

Can you please help me out I am making a simple CRUD project. From node js, I am getting data in JSON format. But I want to transfer that JSON data from EJS to normal inline javascript. Can anyone please help me out with this query.

Thanks in advance.

Which json you are rendering to ejs file you can access it in this way.

router.get("/csv_edit",admin_auth, (req, res) => {
res.render("admin-dashboard", {
    page: {
        name: "edit-csv-main",
        csvId: req.query.csv_id,
        ip: req.ipInfo.ip
    }
});

});

In your javascript file

let r = <%- page %>

How'bout something like...

In nodejs...

res.render('mytemplate', { arrayOfRecs });

In views/mytemplate.ejs...

<%
    arrayOfRecs.forEach(rec => {
        console.log('This is rec: ', rec);
    });
%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM