html - HTML 表单和 NodeJS 的小错误
问题描述
大家好,我现在面临一个小错误,我有两个 html 表单和 nodeJS。并且在 NodeJS 中使用 POST 请求,无论如何它只会发送到其中一个。这是代码
<tr class="digitalLink"><td class="label">Input: Uncompressed GS1 Digital Link URI</td><td>
<form id="signUp"class="form-signUp" action="/compression" method="get" >
<input id="uncompressedDigitalLinkInput" class="digitalLink" type="text" v-model="uncompressedDigitalLinkInput" name="compression"></td>
<td class="charCount">{{uncompressedDigitalLinkInput.length}}</td>
<button type="submit" form="signUp" style="margin-top:30px"
class="btn btn-lg btn-primary btn-block" value="Compressed" >Compressed</button>
</form>
第二种形式:
<tr class="compressedDigitalLink"><td class="label">Input: Compressed GS1 Digital Link URI</td><td>
<form id="signUp"class="form-signUp" action="/decompression" method="post" >
<input id="compressedDigitalLinkInput2" class="compressedDigitalLink" type="text" v-model="compressedDigitalLinkInput2" name="decompression"></td>
<td class="charCount">{{compressedDigitalLinkInput2.length}}</td>
<button type="submit" form="signUp" style="margin-top:30px"
class="btn btn-lg btn-primary btn-block" value="Uncompressed" >Uncompressed</button>
在这里:NodeJS 代码
app.post('/decompression', function(req,resp){ //Post Response
var decompression = req.body.decompression;
console.log("Mon deuxieme test" + decompression);
});
非常感谢你的帮助
解决方案
我认为你在这里有两个问题。
<button type="submit" form="signUp" style="margin-top:30px" class="btn btn-lg btn-primary btn-block" value="Uncompressed" >Uncompressed</button>
两个提交按钮都指向同一个表单id="signUp"
,也就是第一个表单。每个 HTML 元素都应该有一个唯一 ID,您需要为表单指定不同的 ID 并在提交按钮 (form="signUp1", form="signUp2"
) 中引用它们。或者最好您可以删除提交按钮中的表单属性,因为两个提交按钮都在各自的表单中。<form id="signUp"class="form-signUp" action="/decompression" method="post">
我认为这不是action="/decompression"
引用您的 nodejs 端点的有效方法。在您的本地计算机上action="http://localhost:8080/decompression"
,您可能拥有 PORT 可能会因您的情况而异。
希望我的回答有帮助。
推荐阅读
- xml - XML/XSL if else 子字符串
- javascript - 单独更改点大小散点图——ChartJS
- docker - 如何从主机(Windows 机器)连接到 docker 容器
- log4j - 如何为嵌入式 Jetty 服务器配置 log4j.properties?
- ruby-on-rails - 通过 Rmagick 错误将 PDF/DOC/DOX 转换为 PNG 时:没有这样的文件或目录 @error/blob.c/OpenBlob/2712 - Ruby on Rails
- sql - 如何修改我的模式或查询,使其有效运行?
- swiftui - SwiftUI iOS 14 beta TextField 100% CPU
- mysql - AWS lambda 执行中的数据库错误“无法连接到 MySQL 服务器”
- typescript - 传递任何或记录时,TypeScript 中重载匹配顺序的函数签名
- node.js - 用大量数据解决 promise 时阻塞的事件循环