javascript - 向服务器发出请求时我不明白什么
问题描述
我的意图是提出请求方法:“POST”。这是我的应用程序:
.addEventListener("click", async function() {
var newTaskName = document.getElementById("inNewTask").value.trim();
let newTask = { title: newTaskName, sLine: 1 };
const url = "http://localhost:3000/tasks/";
try {
const response = await fetch(url, {
method: "POST",
body: newTask,
headers: {
"Content-Type": "application/json"
},
mode: "no-cors"
});
console.log(newTask);
console.log(response);
return await response;
} catch (error) {
console.error("Error:", error);
}
addTaskToHtml(newTask);
});
这是我在服务器上的解决方案:
router.post("/", (req, res) => {
mongoose
.connect("mongodb://localhost:27017/kanban", {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(connection => {
let task = new Task({
title: req.body.title,
// tag: req.body.tag,
sLine: req.body.sLine
});
task
.save()
.then(task => {
res.send(task);
console.log("Task is there");
})
.catch(err => {
console.log("Something gets wrong");
console.error(err.message);
})
.then(task => {
connection.disconnect();
res.send(task);
});
})
.catch(err => {
res.status(400).send({ err: "there is no database" });
});
});
我尝试了很多解决方案,可能我错过了重要的意义。
我的服务器最终看不到我的Title并且经常发回 undefined。
它没有到达数据库。
解决方案
推荐阅读
- android - 关于从 firebase android studio 中检索创建动态列表视图
- javascript - 三元表达式内的角度插值
- android - 无法在 Qt android 中安排本地通知
- entity-framework - 如何从实体中的数据库 dll 更新模型?
- javascript - 如何从循环中传递 PHP 变量并将其用作 Javascript 变量
- regex - 正则表达式 OR 条件
- javascript - 如何在 jquery/rails 中呈现新字段?
- python - 我怎样才能发现类的重新定义?
- node.js - Node.js 集群;仅在使用颜色时混乱的控制台输出
- git - Github:使用 ssh 克隆有效,但 https 无效