419页在我的laravel应用中过期。我该如何解决?

时间:2020-07-21 05:21:00

标签: php laravel

基本信息
我正在使用Laravel6.0开发一个简单的Web应用。

错误消息
当我打开我的应用程序时,

419 Page Expired

此错误消息出现。
我想正确打开我的应用程序。 我的代码 layout.blade.php

<!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">
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <title>@yield('title')</title>
        <script src="{{ asset('js/app.js') }}" defer></script>
        <link rel="dns-prefetch" href="https://fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css?family=Raleway:300,400,600" rel="stylesheet" type="text/css">
        <link href="{{ asset('css/main2.css') }}" rel="stylesheet">
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
    </head>


<body>
<header>
    <div class="header">
        <div class="titletitle">
            <h1><a href="{{ url('/')}}" class="titlea"><i class="fas fa-hat-wizard"></i> |Title</a></h1>
        </div>
        <div class="titleicon">

            <a href="{{ url('/stories/create') }}"><i class="far fa-images"></i></a>
            <a href="{{ url('/register') }}"><i class="far fa-user-edit"></i></a>
            <a href="{{ url('/profile/create3') }} "><i class="far fa-user"></i></a>


            <ul class="navbar-nav">
                            @guest
                            <li><a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a></li>
                        {{-- ログインしていたらユーザー名とログアウトボタンを表示 --}}
                            @else
                            <li class="nav-item dropdown">

                            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                                <a class="dropdown-item" href="{{ route('logout') }}"
                                      onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                        {{ __('Logout') }}
                                </a>
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>



                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                        @csrf
                                    </form>
                                </div>
                            </li>
                            @endguest
                        </ul>
        </div>


    </div>


</header>
        <!--Profile-->
@yield('content')

<footer>
        <div class="footer">
            <div class="titlefooter"><h1>Title</h1></div>
        </div>
</footer>
    </body>

</html>

index.blade.php

@extends('layouts.front2')
@section('title','mainpage')

@section('content')

<link rel="stylesheet" href="{{ asset('css/main2.css') }}">

<div class="profile">
    <div class="name">
        @guest
        <a class="nav-link2" href="{{ route('register')}}">{{ __('Create Accout!')}}</a>
        @else
        <a id="navbarDropdown" class="nav-link2" href="#" role="button">
            {{ Auth::user()->name }}<span class="caret"></span></a>
        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
            @csrf
        </form>
        @endguest
    </div>

<div class="aboutme">
  <tbody>
    @foreach($posts as $profile)
        <tr>
            <td>{{ \Str::limit($profile->title, 100) }}</td>
            <td>{{ \Str::limit($profile->body, 250) }}</td>
        </tr>
        <a href="{{ action('ProfileController@delete', ['id' => $profile->id]) }}" class="update">delete</a>
        <a href="{{ action('ProfileController@update', ['id' => $profile->id]) }}" class="update">update</a>
    @endforeach
</tbody>
<br>
</div>
</div>

<div class="new">
    <div class="newtitle">
        <h1>New</h1>
    </div>
    
    <div class="container1">
        @foreach ($images as $image)

        <img src="/storage/{{ $image->path . $image->name }}" class="images" style="height: 250px; width: 250px; border-radius: 50%;">

        <a href="{{ action('StoriesController@delete', ['id' => $image->id]) }}" class="update">delete</a>
        @endforeach
    <div class="more">
        more...
    </div>
    </div>
</div>


{{ csrf_field() }}
@endsection

我尝试过的事情。
→我在每个@csrf标签中插入<form>
→我删除了{Laravel project}\storage\framework\sessions 中的所有文件。
但没有任何改变。

我该如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

如果在调用AJAX后收到错误消息,则需要添加如下标题。 在您的头部标签中

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

在您的脚本标签中

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