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.
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
New contributor
mohammadK developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.