简体   繁体   中英

Laravel 5.4 - pjax response an error Unprocessable Entity

I used laravel pjax plugins from laravel/spatie package

When i clicked the Login and Register link (basic laravel auth), it works fined. Pjax is working, but when i tried to login it shows an error .

(1/1) HttpException
in Application.php (line 926)
at Application->abort(422, '', array())
in helpers.php (line 30)
at abort(422)
in FilterIfPjax.php (line 87)
at FilterIfPjax->fetchContainer(object(Crawler), '#body')
in FilterIfPjax.php (line 54)
at FilterIfPjax->filterResponse(object(Response), '#body')
in FilterIfPjax.php (line 35)
at FilterIfPjax->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
...

Something missing in my code ? I have no idea how to fix this error. anyone can explain why this error occurs ?

<script type="text/javascript">
        $(document).ready(function(){
            if ($.support.pjax) {
                $.pjax.defaults.timeout = 2000; // time in milliseconds
                $(document).pjax('a', '#body');
                $(document).on('submit', 'form[data-pjax]', function(event) {
                    $.pjax.submit(event, '#body');
                });
            }
        });
    </script>

Is this the correct way in submitting the forms in using pjax (without reloading the page) ?

Here is my app layout

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

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

    <title>{{ config('app.name', 'Laravel') }}</title>

    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}"></script>
    <script type="text/javascript" src="{{asset('js/jquery.pjax.js')}}"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            if ($.support.pjax) {
                $.pjax.defaults.timeout = 2000; // time in milliseconds
                $(document).pjax('a', '#body');
                $(document).on('submit', 'form[data-pjax]', function(event) {
                    $.pjax.submit(event, '#body');
                });
            }
        });
    </script>
</head>
<body id="body">
    ....
   @yield('content')
</body>
</html>

I figured out when I remove the pjax plugins and tried to login nomally it shows an error

SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

So i realized that i forgot to configure the DB_USERNAME and DB_PASSWORD in .env file .

I put the correct USERNAME and PASSWORD and install back the pjax plugin. It works perfectly. :)

I think FilterIfPjax Class throws the error 422 if there's an error in request.. i'm not sure of that :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM