请求失败,状态代码为 419 -Nextjs 和 Laravel Sanctum

问题描述 投票:0回答:1

我有两个项目,Laravel 8 和 Next.js。当我发送 GET 请求时一切正常,但当我发送 POST 请求时遇到错误。我尝试了各种解决方案,但问题仍然存在。下面是我正在使用的代码。

enter image description here

环境

APP_URL=:http://127.0.0.1:8000
FRONTEND_URL = http://localhost:3000
SESSION_DRIVER=cookie
SESSION_DOMAIN='localhost'
SANCTUM_STATEFUL_DOMAINS='localhost,127.0.0.1'

cors.php


    'paths' => ['*','sanctum/csrf-cookie',],

    'allowed_methods' => ['*'],

    'allowed_origins' => [env('FRONTEND_URL','http://localhost:3000')],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

web.php

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});
Route::post('data/save','HomeController@save_data') ;

前端 axios.ts

const customFetch = axios.create({
    baseURL: 'http://127.0.0.1:8000/' ,
    headers:{
        "X-Requested-With": "XMLHttpRequest",
        'Content-Type': 'application/json',
    },
    withCredentials:true,
    //withXSRFToken: true,
})
  const csrf =  await customFetch.get('sanctum/csrf-cookie');
  const response = await customFetch.post('data/save', visa_data);

我尝试在 axios 文件中使用

withXSRFToken: true, 
但一样

php reactjs axios laravel-8 laravel-sanctum
1个回答
0
投票

添加到 .env (laravel)

SANCTUM_STATEFUL_DOMAINS=http://localhost:3000/

© www.soinside.com 2019 - 2024. All rights reserved.