gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:25:43 24 4
gpt4 key购买 nike

我一直没能找到这个问题的答案。当使用 NodeJS、Express 和 Express Bodyparser - 以及我的其他 MEAN 堆栈时 - 我遇到了隐藏和禁用字段的问题:提交表单时它们不会出现在 req.body 对象中。

如果我没理解错的话,express的bodyparser这方面其实是取自另一个项目的。无论如何,我还没有真正弄清楚为什么要删除这些字段或如何阻止这种情况发生。我怀疑它可能与 methodoverride 有关,但启用/禁用它对相关字段没有影响。

所以.. 对于我自己和 future 的 googler:这里发生了什么?

根据要求,这里有一些代码: 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);

用户路由:

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

最佳答案

这与Express无关。只有成功的控件在提交表单时由浏览器序列化和发送。

The HTML spec defines what makes a control successful.

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.

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.
  • For menus, the control name is provided by a SELECT element and values are provided by OPTION elements. 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.

Hidden controls and controls that are not rendered because of style sheet settings may still be successful.

您的问题实际上是 Angular。 Apparently , 它不设置隐藏输入的 value

关于javascript - 隐藏/禁用字段从 req.body 中消失(Express JS bodyParser),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19639529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com