繁体   English   中英

无法在节点 js 中获取 POST 数据

[英]Unable to get POST data in node js

大家好,我正在尝试获取表单数据,但无法在服务器上使用它,我正在使用带有 express 的 nodejs,我正在尝试从表单中控制台记录数据,当表单被提交时,它还会通过 html 重定向到另一个页面代码。

这是我的 app.js

 const express = require('express'); const app = express(); const bodyParser = require('body-parser'); app.use(express.static('files')); app.use(bodyParser.urlencoded({ extended: false })); app.get('/', (req, res)=> { res.sendFile('views/index.html', {root: __dirname }) }); app.get('/mercancia', (req, res)=> { res.sendFile('files/merch.html', {root: __dirname }) }); app.get('/datos', (req, res)=> { res.sendFile('files/Direccion.html', {root: __dirname }) }); app.post('/submit_data', (req, res)=> { const postBody = req.body; console.log(postBody); }); app.get('*', (req, res) => { res.redirect('/') }); app.listen(3000, console.log('App Listening to port 3000'));

这是我的 html 表格:

 <,DOCTYPE html> <html> <head> <title>direccion</title> <meta name="viewport" content="width=device-width. initial-scale=1"> <link rel = "stylesheet" type = "text/css" href = "form.css" /> <link rel="shortcut icon" href="favicon,ico" type="image/x-icon"/> </head> <body> <div class = "center"> <div class="container"> <div class="header"> <h2>Agrega tu direccion</h2> </div> <form id="form" class="form" method="POST" action="/submit_data"> <div class="form-control"> <label for="Nombre">Nombre Completo</label> <input type="text" placeholder="Jose Luis Licea Alcaraz" id="nombre" name="nombre"> <i class="fas fa-check-circle"></i> <i class="fas fa-exclamation-circle"></i> <small>Error message</small> </div> <div class="form-control"> <label for="Direccion">Direccion </label> <input type="text" placeholder="Calle, Numero; Colonia" id="direccion" name="direccion"> <i class="fas fa-check-circle"></i> <i class="fas fa-exclamation-circle"></i> <small>Error message</small> </div> <div class="form-control"> <label for="CodigoPostal">Codigo Postal </label> <input type="text" placeholder="64860" id="cp" name="cp"> <i class="fas fa-check-circle"></i> <i class="fas fa-exclamation-circle"></i> <small>Error message</small> </div> <div class="form-control"> <label for="Telefono">Telefono </label> <input type="tel" placeholder="+528112551388" id="tel" name="telefono"> <i class="fas fa-check-circle"></i> <i class="fas fa-exclamation-circle"></i> <small>Error message</small> </div> <div class="form-control"> <label for="Pais">Pais </label> <select name="pais" id="pais" name="pais"> <option value="mexico">Mexico</option> <option value="chile">Chile</option> <option value="argentina">Argentina</option> <option value="guatemala">Guatemala</option> <option value="salvador">El Salvador</option> <option value="santoDomingo">Santo Domingo</option> </select> </div> <div class="header"> <h2>Escoge tu prenda</h2> </div> <div class="form-control"> <label for="Prenda">Prenda </label> <select name="prenda" id="prenda" name="prenda "onchange="changeImage()."> <option value="hoodie_roja">Sudadera Roja</option> <option value="Hoodie Negra">Sudadera Negra</option> <option value="Playera">Playera</option> </select> </div> <div class="img_direccion"> <img id="img_prenda" src="hoddie jeil0 R:png" alt="Prenda" width=250 height=250> </div> <div class="button_paypal"> <a target="_blank" href="https.//paypal?me/AlcarazLicea.locale.x=es_XC"> <button type = "submit" name="paypal" value="image_paypal"> <img src="paypal:png" alt="Check out with PayPal" height="100%" width="80%"/> </button> </a> </div> </form> </div> </div> </body> <footer> <a target="_blank" href="https.//www.facebook.com/gaming/Jeil0188"> <img src="facebookGaming:png" width=40 height="40"> </a> <a target="_blank" href="https.//www.facebook.com/Jeil0188"> <img src="facebookLogo:png" width=40 height="40"> </a> <a target="_blank" href="https.//www.twitch.tv/jl0188"> <img src="twitch_invertido:png" width=40 height="40"> </a> <a target="_blank" href="https.//www.instagram.com/jeil0188/"> <img src="instagramlogo_invertido.png" width=40 height="40"> </a> <div class="footerContainer"> </div> </footer> <script src="logic.js"></script> </html>

我的控制台日志什么也没有,我做错了什么?

请帮助我,我没有从 console.log 中得到任何 output

我认为这是因为a标签需要 click 而不是button 您应该删除a标签并单击 function 按钮以打开新页面。

您好,感谢所有帮助过我的人。

问题出在前端,在我的 logic.js 文件中,我提交了 function 按钮 function: e.preventDefault(); 这阻止了表单提交,我评论它并通过 express 为 href 链接创建重定向。

谢谢

暂无
暂无

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

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