2

I have two projects, Laravel 8 and Next.js. Everything works smoothly when I send a GET request, but I encounter an error when I send a POST request. I've attempted various solutions, but the issue persists. Below is the code I'm using.

enter image description here

env

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

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

I tried to usewithXSRFToken: true, in axios file but same

1 Answer 1

0

Add to .env (laravel)

SANCTUM_STATEFUL_DOMAINS=http://localhost:3000/
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.