Intro-to-Data-APIs-JS icon indicating copy to clipboard operation
Intro-to-Data-APIs-JS copied to clipboard

HTTP POST request with fetch() method not allowed

Open irimina opened this issue 2 years ago • 0 comments

Post with fetch() does not work on my localhost on both Apple and Windows. This line of code const response = await fetch("/api", options); throws a 405 error saying that the method is not allowed. So, no data is sent to the server. Any ideas? Here is the larger snippet let lat, lon; const button = document.getElementById("submit"); button.addEventListener("click", async (event) => { const data = { lat, lon }; const options = { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(data), }; const response = await fetch("/api", options); const json = await response.json(); console.log(json); });

irimina avatar Aug 05 '23 20:08 irimina