首页 > 解决方案 > DELETE 用户 Express 端可以工作,但不能使其在前端工作(使用 React)

问题描述

我在服务器端(Express)创建了我的路由用户/删除,我通过 Postman 对其进行了测试并且没有任何问题,但不知何故我无法让它在前端工作。

这是我的反应代码,我正在尝试使用 axios 创建从 express 中删除和获取路由的功能:

  const handleDelete = () => {
    axios
      .delete("http://localhost:9090/users/delete", {})
      .then(response => response.data)
      .catch(error => {
        throw error.response.data;
      });
  };

这是我使用该功能的方式:

  <button type="button" onClick={handleDelete}>
    Delete your account
  </button>
</div>

我还是这项技术的新手,所以我不确定什么是正确的方法。

标签: mysqlnode.jsreactjsexpressaxios

解决方案


查看评论,我相信问题可能出在isAuthenticated您正在使用的中间件上,因为当身份验证中间件失败时,它会引发403 Forbidden错误。您可能需要传递一些令牌,以在axios通话中验证您的请求。

const request = await axios.delete(`http://localhost:9090/users/${userId}`,{header})

如果您可以提供有关中间件的更多信息,我将更新我的答案。


推荐阅读