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

问题描述

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



解决方法

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