window.axios = require('axios');
window.axios.defaults.headers.common = {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
};
axios.post('/ajax/post', {
id: 9999,
}).then(response => {
}).catch(function (resp) {
console.log(resp);
});
protected $middlewareGroups = [
'web' => [],
'api' => [],
'js' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
AjaxVerifyCsrfToken::class,
SubstituteBindings::class,
]
];
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class AjaxVerifyCsrfToken extends Middleware
{
protected $addHttpCookie = true;
protected $except = [
//
];
public function handle($request, Closure $next)
{
if (
$this->runningUnitTests() || (
$this->isReading($request) &&
$this->tokensMatch($request)
)
) {
return tap($next($request), function ($response) use ($request) {
if ($this->shouldAddXsrfTokenCookie()) {
$this->addCookieToResponse($request, $response);
}
});
}
throw new TokenMismatchException('Несоответствие CSRF ключа. Перезагрузите страницу.');
}
protected function runningUnitTests(): ?bool
{
return env('APP_ENV') === 'testing';
}
protected function isReading($request): ?bool
{
return in_array($request->method(), ['GET', 'POST', 'HEAD', 'GET', 'OPTIONS']);
}
}
Array
(
[v] => 1
[tid] => UA-xxxxxxxxx-x
[cid] => xxxxxxxxxx.xxxxxxxxxx
[ti] => 1531030
[t] => event
[ec] => Ecommerce
[ea] => Refund
[ni] => 1
[pa] => refund
[cu] => RUB
)
Ответ не решает 500 ошибку, но предлагает другое решение, при том вполне рабочее. Возможно есть более правильный подход. Подскажи, пожалуйста, где можно посмотреть на него?