Nextjs: 405 when form submit POST

Viewed 719

Following this tutorial, I'm trying to submit a form in Nextjs but get a 405 error when trying to POST to my api route. I've tried using both axios and the fetch api. This seems like it should be simple to do but I'm not sure where I'm going wrong. Anything obvious I'm missing? Thanks!

/survey/create

const handleSubmit = async (event) => {
    event.preventDefault();

    const testData = {
      title: surveyTitle,
    };

    const res = await fetch("/api/survey", {
      method: "POST",
      body: JSON.stringify({
        dataName: testData,
      }),
      headers: {
        "Content-Type": "application/json",
      },
    });
  };

/api/survey

export default function handler(req, res) {
  console.log(req.body);
  res.status(200).json(JSON.stringify({ name: 'Success' }))
}
0 Answers
Related