提取响应没有被处理

发布于 2025-02-07 02:50:40 字数 684 浏览 0 评论 0原文

我正在创建一个看起来与此相似的获取请求:

fetch('/endpoint', {
      method: "POST",
      headers: {"Content-Type": "application/json; charset=UTF-8"},
      body: JSON.stringify(data)
    })
    .then((response) =>{
      if(response.ok){
        console.log("Response was ok");
        back();
      }
     else {
      const result = response.json();
      result.then((result) => alert(result.message))
     }
    })
    .catch(err => console.log(err))
  }

供参考,这是后背函数:

const back = () => {
    navigate('/dashboard')
  }

我希望用户在请求完成后将用户路由回到仪表板组件。当仅绑在按钮上时,后背函数正确执行,如果响应存在问题,则该问题确实显示为警报。但是,为什么包含控制台日志的块和back()在响应还可以时不执行?

I'm creating a fetch request, that looks similar to this:

fetch('/endpoint', {
      method: "POST",
      headers: {"Content-Type": "application/json; charset=UTF-8"},
      body: JSON.stringify(data)
    })
    .then((response) =>{
      if(response.ok){
        console.log("Response was ok");
        back();
      }
     else {
      const result = response.json();
      result.then((result) => alert(result.message))
     }
    })
    .catch(err => console.log(err))
  }

For reference, this is the back function:

const back = () => {
    navigate('/dashboard')
  }

I would like the user to be routed back to a dashboard component after the request is complete. The back function executed properly when just tied to a button, and if there is an issue with the response, the issue does display as an alert. But why does the block containing the console log and back() not execute when the response is ok?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2025-02-14 02:50:40

我意识到问题在后端。响应是返回状态,但没有jsonified对象

router.delete('/route', controller.delete, (req, res) => {
  return res
  .set('Content-Type', 'application/json')
  .status(200)
})

之后


router.delete('/route', controller.delete, (req, res) => {
  return res
  .set('Content-Type', 'application/json')
  .status(200).json();
})

I realized that the problem was in the back end. The response was returning a status, but no jsonified object:

router.delete('/route', controller.delete, (req, res) => {
  return res
  .set('Content-Type', 'application/json')
  .status(200)
})

Before


router.delete('/route', controller.delete, (req, res) => {
  return res
  .set('Content-Type', 'application/json')
  .status(200).json();
})

After

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文