'fecth api not posting data to nodeserver
i have a node backend set up, when i send a get request, it works perfectly, but when i send a post request it doesn't send the data to the backend. Here is my code:
fetch("http://localhost:3000/", { method: "POST",body:{"title":title.value,"content":content.value}})
.then((res) => res.json())
.then((data) => {
console.log(data);
});
i have even tried sending it through the formdata object it is still not working:
const formdata = new FormData();
formdata.append("title",title.value)
formdata.append("content",content.value)
fetch("http://localhost:3000/", { method: "POST",body:formdata})
.then((res) => res.json())
.then((data) => {
console.log(data);
});
this is the console.log: enter image description here
this is the backend:
//crud applications
app.post("/", async (req, res) => {
const { title, content } = req.body;
try {
const newPost = await postModel.create({ title, content });
res.json(newPost);
} catch (error) {
res.status(500).send(error);
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
