简体   繁体   English

将数据从 javascript 变量传递到 ejs 变量

[英]passing data from javascript variable to an ejs variable

i am getting a value in the variable items and this is in a script tag in html but i want to pass the data in the variable items to my back end to be used for storing in a db.我在变量项中获得一个值,这是在 html 中的脚本标记中,但我想将变量项中的数据传递到我的后端以用于存储在数据库中。 i made selectedTeams a res.local.selectedTeams so i can use it in the ejs to try store the value of items and use it like that but it does not work我将 selectedTeams 设为 res.local.selectedTeams,因此我可以在 ejs 中使用它来尝试存储项目的值并像这样使用它,但它不起作用

var transfer = $("#transfer1").transfer(settings1);
    // get selected items
    var items = transfer.getSelectedItems()
    console.log(items)
    for (let index = 0; index < items.length; index++) {
      <%= selectedTeams %>.push({_id: items[index].value})
    }


 res.locals.selectedTeams = []


router.post('/newseason', auth.adminAuth, async function(req, res, next) {
  console.log('..............................................................')
  console.log(selectedTeams)
});

so when the url gets called /newseason i would like the selected teams to be in the selected teams viariable因此,当 url 被称为 /newseason 时,我希望选定的球队在选定的球队中

You need to send ajax request to the api and pass the data with the api so it will be sent to the server.您需要向api发送ajax请求并使用api传递数据,以便将其发送到服务器。 And from API in the backend code you can access selectedTeams in req.body .而从后端代码API,您可以在访问selectedTeams req.body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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