简体   繁体   English

隐藏/禁用字段从req.body消失(Express JS bodyParser)

[英]Hidden/Disabled fields disappear from req.body (Express JS bodyParser)

I have not been able to find the answer to this question. 我一直无法找到这个问题的答案。 When using NodeJS, Express, and the Express Bodyparser - and the rest of my MEAN stack for that matter - I run into an issue with both hidden and disabled fields: they do not show up in the req.body object when submitting a form. 当使用NodeJS,Express和Express Bodyparser - 以及我的MEAN堆栈的其余部分 - 我遇到了隐藏和禁用字段的问题:提交表单时它们不会出现在req.body对象中。

If I understand correctly, the bodyparser aspect of express is actually taken from another project. 如果我理解正确,快递的bodyparser方面实际上是从另一个项目中获取的。 Regardless, I have not really been able to figure out why these fields are being removed or how to stop this from happening. 无论如何,我还没有真正弄清楚为什么要删除这些字段或如何阻止这种情况发生。 I suspect it might have something to do with methodoverride, but enabling/disabling this has no effect on the fields in question. 我怀疑它可能与methodoverride有关,但是启用/禁用它对相关字段没有影响。

So.. for myself and future googler's: what is happening here? 那么......对于我自己和未来的googler's:这里发生了什么?

Here's some code, as requested: JADE: 根据要求,这里有一些代码:JADE:

form(action="/admin/users/edit", method="post", ng-submit='registerUser($event)', name='form', novalidate)
    .row(ng-show="ifweareupdating")
        label(for="_id") _id:
        input(type="hidden", id="_id", name="_id", ng-model="newUser._id")
        input(type="text", id="_notHiddenId', name="_notHiddenId", ng-model="newUser._id")

EXPRESS: 表达:

app.post("/admin/users/edit", pass.ensureAdmin, userRoutes.editUserPost);

userRoutes: userRoutes:

exports.userRoutes.editUserPost = function(req,res,next) { 
    console.log(req.body._id) // logs undefined
    console.log(req.body._notHiddenId) // logs actual id
}

This has nothing to do with Express. 这与Express无关。 Only successful controls are serialized and sent by the browser when a form is submitted. 在提交表单时,浏览器仅序列化并发送成功的控件

The HTML spec defines what makes a control successful. HTML规范定义了控件成功的原因。

A successful control is "valid" for submission. 成功的控制对于提交是“有效的”。 Every successful control has its control name paired with its current value as part of the submitted form data set. 每个成功的控件都将其控件名称与其当前值配对,作为提交的表单数据集的一部分。 A successful control must be defined within a FORM element and must have a control name. 必须在FORM元素中定义成功的控件,并且必须具有控件名称。

However: 然而:

  • Controls that are disabled cannot be successful. 已禁用的控件无法成功。
  • If a form contains more than one submit button, only the activated submit button is successful. 如果表单包含多个提交按钮,则只有激活的提交按钮成功。
  • All "on" checkboxes may be successful. 所有“打开”复选框都可能成功。
  • For radio buttons that share the same value of the name attribute, only the "on" radio button may be successful. 对于共享相同name属性值的单选按钮,只有“on”单选按钮可能成功。
  • For menus, the control name is provided by a SELECT element and values are provided by OPTION elements. 对于菜单,控件名称由SELECT元素提供,值由OPTION元素提供。 Only selected options may be successful. 只有选定的选项可能会成功。 When no options are selected, the control is not successful and neither the name nor any values are submitted to the server when the form is submitted. 如果未选择任何选项,则控件不会成功,并且在提交表单时,不会将名称或任何值提交给服务器。
  • The current value of a file select is a list of one or more file names. 文件选择的当前值是一个或多个文件名的列表。 Upon submission of the form, the contents of each file are submitted with the rest of the form data. 提交表单后,每个文件的内容将与其余表单数据一起提交。 The file contents are packaged according to the form's content type. 文件内容根据表单的内容类型打包。
  • The current value of an object control is determined by the object's implementation. 对象控件的当前值由对象的实现决定。

If a control doesn't have a current value when the form is submitted, user agents are not required to treat it as a successful control. 如果控件在提交表单时没有当前值,则用户代理不需要将其视为成功控件。

Furthermore, user agents should not consider the following controls successful: 此外,用户代理不应该认为以下控件成功:

  • Reset buttons. 重置按钮。
  • OBJECT elements whose declare attribute has been set. 已设置declare属性的OBJECT元素。

Hidden controls and controls that are not rendered because of style sheet settings may still be successful. 由于样式表设置而未呈现的隐藏控件和控件仍可能成功。

Your problem is actually Angular. 你的问题实际上是Angular。 Apparently , it does not set the value of hidden inputs. 显然 ,它没有设置隐藏输入的value

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

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