在 Laravel 中找不到 CSRF 令牌

Dkn*_*kna 1 csrf laravel

我收到此 CSRF 令牌错误,当我环顾四周时,每个人都说我只需要在提交数据时使用 {{ csrf_field() }}。过去,它运行良好,但一段时间后,出现此错误,我无法提交数据,甚至根本无法编辑数据。有人可以在这里帮助我吗谢谢。

在此处输入图片说明

请告诉我我哪里出错了?我想在这里做什么

edit.blade.php(这是 csrf 部分)

        <form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object->id) }}">

          {{ method_field('PUT')  }}
          {{ csrf_field() }}

         <div class="input-group">
            <label><b>Name/NRIC:</b></label>
              <input type="text" name="Name" value="{{ $object->Name }}" class="form-control">
        </div>
</form>
Run Code Online (Sandbox Code Playgroud)

app.js(laravel 默认不是我的,这是我看到的)

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = __webpack_require__(16);

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

/**
 * Next we will register the CSRF Token as a common header with Axios so that
 * all outgoing HTTP requests automatically have it attached. This is just
 * a simple convenience so we don't have to attach every token manually.
 */

var token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
  window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
  console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
Run Code Online (Sandbox Code Playgroud)

编辑: 不确定这是否相关,但由于它是登录部分,只是想知道它是否会影响这一点。

我实际上有 2 个 app.blade.php 用于布局,所以想知道导致整个问题的原因是什么?

Leo*_*ent 5

转到您的主刀片或app.blade.php确保添加以下内容:

<meta name="csrf-token" content="{{ csrf_token() }}">
Run Code Online (Sandbox Code Playgroud)