LINUX.ORG.RU

Laravel генерирует ошибку при обработке AJAX-запроса

 , ,


0

1

Laravel 5.5

Пытаюсь запустить тестовый пример вот отсюда:

https://webformyself.com/laravel-ajax/

Но он не работает.

В логе веб-сервера вижу, что POST-запрос приходит. В браузере вижу, что Laravel попыталась обработать AJAX-запрос, но не смогла. Ответное сообщение приходит, но не с маленьким JSON-ответом, а с портянкой ошибки.

Судя по портянке, до контроллера выполнение не доходит, потому что нет никакого упоминания про AjaxController.php. То есть, что-то происходит в глубинах Laravel, но я не могу понять что.

Вопрос: как заставить работать этот пример?

Код ответного сообщения:

{
    "message": "",
    "exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException",
    "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php",
    "line": 203,
    "trace": [
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php",
            "line": 175,
            "function": "prepareException",
            "class": "Illuminate\\Foundation\\Exceptions\\Handler",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/app/Exceptions/Handler.php",
            "line": 51,
            "function": "render",
            "class": "Illuminate\\Foundation\\Exceptions\\Handler",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 83,
            "function": "render",
            "class": "App\\Exceptions\\Handler",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 55,
            "function": "handleException",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php",
            "line": 49,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\View\\Middleware\\ShareErrorsFromSession",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
            "line": 63,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Session\\Middleware\\StartSession",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php",
            "line": 37,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php",
            "line": 66,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Cookie\\Middleware\\EncryptCookies",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 102,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 660,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 635,
            "function": "runRouteWithinStack",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 601,
            "function": "runRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 590,
            "function": "dispatchToRoute",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 176,
            "function": "dispatch",
            "class": "Illuminate\\Routing\\Router",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 30,
            "function": "Illuminate\\Foundation\\Http\\{closure}",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/fideloper/proxy/src/TrustProxies.php",
            "line": 56,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Fideloper\\Proxy\\TrustProxies",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 30,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 30,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
            "line": 27,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
            "line": 46,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 149,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\\Pipeline\\{closure}",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 102,
            "function": "Illuminate\\Routing\\{closure}",
            "class": "Illuminate\\Routing\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 151,
            "function": "then",
            "class": "Illuminate\\Pipeline\\Pipeline",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 116,
            "function": "sendRequestThroughRouter",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        },
        {
            "file": "/media/wd_640_part_2/home64bit/xi/work/develop/php/tukituk/site/public/index.php",
            "line": 55,
            "function": "handle",
            "class": "Illuminate\\Foundation\\Http\\Kernel",
            "type": "->"
        }
    ]
}

★★★★★
Ответ на: комментарий от fman2

Новые версии Laravel, похоже, токен в данных AJAX запроса не понимают.

Теперь токен можно передавать в заголовке:

<meta name="csrf-token" content="{{ csrf_token() }}">

...

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});


Или без метатега:

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="{{ csrf_token() }}"]').attr('content')
  }
});

Xintrea ★★★★★
() автор топика
Ответ на: комментарий от Xintrea

Все верно, у меня токен передается через X-XSRF-TOKEN. Но я использую axios, там уже подумали за меня.

В итоге, получилось?

fman2
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.