简体   繁体   English

节点JS:将对象从HTML传递到服务器

[英]Node JS: Pass object from HTML to Server

I'm currently passing in an object into my ECT html file (same as EJS just for coffeescript) which has a form. 我目前正在将对象传递到具有形式的ECT html文件中(与用于咖啡脚本的EJS相同)。 This object contains a business name, business id and visit id and I only display the business name inside my ECT file. 该对象包含公司名称,公司ID和访问ID,而我仅在ECT文件中显示公司名称。

I'm trying to pass the business object into the body with my post request that contains all the form details. 我正在尝试通过包含所有表单详细信息的发布请求将业务对象传递到正文中。 I've tried the whole data-* ( http://html5doctor.com/html5-custom-data-attributes/ ) but that isn't working. 我已经尝试了整个数据-*( http://html5doctor.com/html5-custom-data-attributes/ ),但是那没有用。

This is what my form looks like right now 这是我的表格现在的样子

<form id="form" action="/answer" method="post" data-biz="<%= @business%>">
# Inputs here

However, my request body only contains the values for my inputs and nothing with the business object. 但是,我的请求正文仅包含我输入的值,而没有包含业务对象。 Is there any way to pass my business object back into my post request for /answer? 有什么方法可以将我的业务对象传递回我对/ answer的帖子请求中?

Thanks! 谢谢!

You can JSON.stringify the object and pass it through a hidden input: 您可以JSON.stringify该对象并将其通过隐藏的输入传递:

<form id="form" action="/answer" method="post">
  <input name="name" value="<%= @business.name %>"/>
  <input name="business" type="hidden" value="<%= JSON.stringify(@business) %>"/>
</form>

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

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