【问题标题】:The server responded with a status of 404 (Not Found) problem服务器响应状态为 404(未找到)问题
【发布时间】:2021-07-15 08:32:42
【问题描述】:

我的项目遇到了 404 问题。

Failed to load resource: the server responded with a status of 404 (Not Found)

我不知道问题出在哪里。我是 MERN 堆栈的新手。我该如何解决这个问题?

这是我的 POST API

router.post("/", async (req, res) => {
  try {
    const _id = await getNextSequence("courier")
    req.body.id = _id    
    
    const courier = new CourierInfo(req.body)
    await courier.save()
    res.sendStatus(200)
  } catch (error) {
    
    res.sendStatus(500)
  }
})

这是前端的create方法。

// create method
  const submit = async (e, { resetForm }) => {       
      try {
        setCallingBackend(true)
        await axios.post('/courier', e)
        resetForm()
        setAlert({
          showAlert: true,
          severity: 'success',
          message: 'Courier information created successfully!',
        })
      } catch (error) {
        if (error.response.status === 422) {
          setAlert({
            showAlert: true,
            severity: 'error',
            message: 'Courier information already exists!',
          })
        } else {
          console.log(error)
          setAlert({
            showAlert: true,
            severity: 'error',
            message: 'Courier information creation failed!',
          })
        }
      } 
  }

  

【问题讨论】:

    标签: reactjs api http-status-code-404 mern


    【解决方案1】:

    我认为您的端点不一样。在前端,您使用的是“/courier”,而在后端,您只有“/”。

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 2023-04-10
      • 2015-12-01
      相关资源
      最近更新 更多