表单只是在提交时刷新页面,不去路由
Form just refreshes page on submit and does not go to routes
我已经为此苦苦挣扎了很长时间,无法弄清楚问题出在哪里。我只想 POST 到 /accountRoutes/editFinalMark
并且我已经包含了路线和控制器的代码。
<form method="post" action="/accountRoutes/editFinalMark">
<fieldset class="form-group">
<div class="row justify-content-md-center">
<div class="form-group" >
<input type="" id="moduleID" name="moduleID" style="display: none;" value="<%= module._id %>">
<input type="" name="editFinalMarkInput" id="editFinalMarkInput" class="urlInput" placeholder="Add Final Mark" style="">
</div>
<div class="form-group">
<input type="submit" name="editFinalMarkButton" id="editFinalMarkButton" class="submit-docs" value="Add" style="background-color: #6dabe4; border-top-right-radius: 15px; border-bottom-right-radius: 15px; color: white;" />
</div>
</div>
</fieldset>
</form>
// The Route:
router.post('/editFinalMark', accountController.editFinalMark_post);
// The Controller I want to call:
module.exports.editFinalMark_post = async (req, res) => {
try {
const { editFinalMarkInput, moduleID } = req.body;
console.log("\n\n\n\n\n");
console.log("editFinalMarkInput: ", editFinalMarkInput);
console.log("moduleID: ", moduleID);
Module.findOneAndUpdate(
{ _id: moduleID },
{ $push: { final_mark: editFinalMarkInput } },
function (error, success) {
if (error) {
console.log(error);
} else {
console.log(success);
}
}
);
res.status(201).json({ });
} catch (err) {
res.status(400).json({ err })
}
};
当我点击提交时发生的所有事情是页面重新加载并且 url 更新以包含表单中的数据,但它没有完成表单中指定的操作。控制台也完全没有错误。
All that happens when I click submit is that the page reloads and the url updates to include the data from the form, but it doesn't complete the action specified in the form.
所以它忽略了action
(因为它走错了URL)和method
(因为它把数据放在了URL所以它正在发出 GET 请求)。
据此我们可以推断您有 另一个 <form>
元素,这就是正在提交的元素。您只是没有将它包含在问题的代码中。
<form>
元素不允许嵌套,因此 validator 可以帮助您找到它。
我已经为此苦苦挣扎了很长时间,无法弄清楚问题出在哪里。我只想 POST 到 /accountRoutes/editFinalMark
并且我已经包含了路线和控制器的代码。
<form method="post" action="/accountRoutes/editFinalMark">
<fieldset class="form-group">
<div class="row justify-content-md-center">
<div class="form-group" >
<input type="" id="moduleID" name="moduleID" style="display: none;" value="<%= module._id %>">
<input type="" name="editFinalMarkInput" id="editFinalMarkInput" class="urlInput" placeholder="Add Final Mark" style="">
</div>
<div class="form-group">
<input type="submit" name="editFinalMarkButton" id="editFinalMarkButton" class="submit-docs" value="Add" style="background-color: #6dabe4; border-top-right-radius: 15px; border-bottom-right-radius: 15px; color: white;" />
</div>
</div>
</fieldset>
</form>
// The Route:
router.post('/editFinalMark', accountController.editFinalMark_post);
// The Controller I want to call:
module.exports.editFinalMark_post = async (req, res) => {
try {
const { editFinalMarkInput, moduleID } = req.body;
console.log("\n\n\n\n\n");
console.log("editFinalMarkInput: ", editFinalMarkInput);
console.log("moduleID: ", moduleID);
Module.findOneAndUpdate(
{ _id: moduleID },
{ $push: { final_mark: editFinalMarkInput } },
function (error, success) {
if (error) {
console.log(error);
} else {
console.log(success);
}
}
);
res.status(201).json({ });
} catch (err) {
res.status(400).json({ err })
}
};
当我点击提交时发生的所有事情是页面重新加载并且 url 更新以包含表单中的数据,但它没有完成表单中指定的操作。控制台也完全没有错误。
All that happens when I click submit is that the page reloads and the url updates to include the data from the form, but it doesn't complete the action specified in the form.
所以它忽略了action
(因为它走错了URL)和method
(因为它把数据放在了URL所以它正在发出 GET 请求)。
据此我们可以推断您有 另一个 <form>
元素,这就是正在提交的元素。您只是没有将它包含在问题的代码中。
<form>
元素不允许嵌套,因此 validator 可以帮助您找到它。