我如何修复 React 和 laravel sanctum 中的 401 未授权错误

How do i fix 401 unauthorized error in react and laravel sanctum

你好,我在 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),
)),

这是我的 React 前端

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);
  });
 });
};  



我发现了这个错误, 我尝试用 post 方法删除一些东西, 所以我只需要将 post 更改为删除方法