【问题标题】:How do i fix 401 unauthorized error in react and laravel sanctum我如何在反应和 laravel sanctum 中修复 401 未经授权的错误
【发布时间】:2021-08-31 10:58:07
【问题描述】:

您好,我在 react 和 laravel sanctum 中遇到问题,在 postman 上测试时运行良好,但在 react 中实现时显示错误 401 Unauthorized

我把它放在我的 .env laravel 文件中

SANCTUM_STATEFUL_DOMAINS=localhost:3000 SESSION_DRIVER=cookie SESSION_DOMAIN=localhost

这是我的有状态域

'stateful' => explode(',', env(
    'SANCTUM_STATEFUL_DOMAINS',
    'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1,' . parse_url(env('APP_URL'), PHP_URL_HOST),
)),

这是我的反应前端

export const postLogout = (token) => (dispatch) => {
const config = {
  headers: { Authorization: "Bearer " + token },
};

console.log(config);
 axios.defaults.withCredentials = true;
 axios.get("http://localhost:8000/sanctum/csrf-cookie").then((response) => {
axios
  .post(`${Api}/logout`, config)
  .then((res) => {
    console.log(res);
    dispatch({
      type: POST_LOGOUT,
      payload: res,
    });
  })
  .catch((err) => {
    console.log(err);
  });
 });
};  



【问题讨论】:

    标签: reactjs laravel http-status-code-401 unauthorized laravel-sanctum


    【解决方案1】:

    我发现了错误, 我尝试使用 post 方法删除某些内容, 所以我只需要更改帖子以删除方法

    【讨论】:

      猜你喜欢
      • 2022-08-07
      • 2020-08-08
      • 2020-12-09
      • 2020-01-24
      • 1970-01-01
      • 2019-01-12
      • 2019-07-10
      • 2018-10-23
      • 1970-01-01
      相关资源
      最近更新 更多