Node Express:axios 显示请求失败,状态代码为 404,即使请求已完成(重定向失败)

Node Express: axios shows Request failed with status code 404, even if the request was completed(failed to redirect)

我正在使用 axios 向 Express 路由发送 post / put 请求。

const handleSubmit = async (e) =>
    {
        e.preventDefault();
        const data = new FormData(e.currentTarget);

        const body = {
            title: data.get('title'),
            description: data.get('description'),
        };

        await axios.put(`${process.env.NEXT_PUBLIC_DR_HOST}/view/${_id}`, body)
    };
    //example link: http://localhost:3000/view/61e038051b755034d31d49a2

放置路线

router.put("/:id", async (req, res) =>
{
    const { id } = req.params;
    await Declaration.findByIdAndUpdate(id, req.body)
    res.redirect("/")
})

post

const handleSubmit = async (e) =>
    {
        e.preventDefault();
        const data = new FormData(e.currentTarget);

        const body = {
            title: data.get('title'),
            description: data.get('description'),
        };

        await axios.post(process.env.NEXT_PUBLIC_DR_HOST, body)
    };

post路线

router.post('/', async (req, res) =>
{
    const declaration = new Declaration({ ...req.body })
    await declaration.save();
    res.redirect("/")
})

现在,当提交到 put 表单时,它显示此错误:未处理的运行时错误 错误:请求失败,状态代码为 404,但实际上请求已完成并保存了模型,即使重定向从未发生并且错误页面仍然存在。当提交到post表单时,No Error,和第一个类似,请求完成但没有重定向,并且表单页面保持不变。 我该如何解决这个问题?

使用 axios

  await axios.post(process.env.NEXT_PUBLIC_DR_HOST, body)
        .then((res) =>
        {
            window.location = res.request.responseURL;
        })