javascript - 在 Node JS 中使用 post 方法时如何修复“重新加载页面循环”?
问题描述
给定以下html表单:
<form class = "new-date" method = "POST" action = "http://localhost:5600/postDate">
<h3>Owner</h3>
<input type ="text" name = "ownerName" class = "form-element global" id="owner">
</select>
<h3>Pet</h3>
<input type = "text" name = "petName" class = "form-element global" id = "pet">
</select>
<h3>Schedule the date</h3>
<input type="date" id="birthday" name="birthday" class = "form-element global" id="date">
<h3>Description</h3>
<textarea class = "form-element description-text" placeholder= "What is wrong with your pet?" name ="problem" ></textarea id="problem"><br>
<input type="submit" class = "form-element btn" value="Add">
</form>
我试图创建一个位于我的 server.js (节点)内的 post 方法:
const exp = require('express');
const path = require('path');
var bodyParser = require('body-parser');
const app = exp();
/*DATABASE AND QUERIES*/
const mysql = require("mysql2");
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "admin",
database: "control_clientes",
connectionLimit: 5
});
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.post('/postDate', (req, res)=>{
console.log(req.body.ownerName); //TO CHECK IF IM RECEIVING THE DATA. IT WORKS
});
提交客户端数据并将其发送到我的服务器后,html 页面未正确执行重新加载任务:
从这张图片中可以看出,重新加载的事情仍在进行。我不确定它为什么会这样。
我是 Node JS 的新手,你知道未完成的重新加载是怎么回事吗?
解决方案
如果您希望浏览器停止旋转(加载),您需要从服务器发送响应。例如,使用 req.body(回显端点)发送 json 响应
app.post('postDate', (req, res) => {
console.log(req.body.ownerName);
res.json(req.body);
})
推荐阅读
- javascript - 无法访问 React-VictoryJs 中的 API 数据
- reporting-services - 如何以编程方式在 SSRS 报告项目中添加 .rdl 文件?
- kotlin - 如何用gson序列化实现接口的类?
- javascript - iPhone 12 Pro LiDAR API 访问 Safari
- java - 在 ormlite ConnectionSource 中创建连接时如何设置超时?
- python - 将多维数组的列格式化为一维数组
- javascript - 承诺问题
- python - MySQL 工作台未启动 加载“致命 Python 错误:initfsencoding:无法加载文件系统编解码器”时出错
- html - 为什么图像和文本不在@media 查询中我的代码的 flexbox 小节中居中?
- mongodb - mongoDB白名单IP