I am having a hard time getting more into the POST method - my teacher set up a server, but it doesn't seem to accept the data I'm sending through. Would the following be an acceptable way of sending my 2 inputs with the type of text? Or is there another standardized way of doing this?
For the sake of the example, my input fields would be first and last name. Nothing crazy.
document.getElementById("myForm").addEventListener("submit", async (e) => {
e.preventDefault();
await fetch("http://localhost:3000/course/add", {
method: "POST",
headers: {
"Content-Type": "multipart/form-data",
},
body: new FormData(e.target),
})
.then((confirm) => confirm.json())
.catch((err) => err)
.then(
(response) =>
(document.getElementById("container").innerHTML = response.message)
);
});
Why it's not working is hard to say without knowing the API implementation. Some pointers for debugging: