在 Laravel 中找不到 CSRF 令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48183416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:13:44  来源:igfitidea点击:

CSRF token not found in laravel

laravelcsrf

提问by Dkna

I am getting this CSRF token error and when I look around everyone just said that all I need is to use {{ csrf_field() }} when I submit my data. In the past, it was working fine but after some time, I got this error and I can't submit my data or even edit my data at all. Can someone help me here thanks.

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

enter image description here

在此处输入图片说明

Please tell me where did I go wrong? What am I suppose to do here

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

edit.blade.php (this is the csrf part)

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>

app.js (laravel default not mine, this is what I saw)

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');
}

EDIT:Not sure if this is related but since it is the login part just want to know if it affects this.

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

I have actually 2 app.blade.php for the layout so was wondering is that the reason which is causing this whole problems?

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

回答by Leo

Go to your master blade or app.blade.phpand make sure you add this :

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

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

回答by Adnan Mumtaz

you need to add this into your head tag

你需要把它添加到你的头标签中

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

this error will not be shown anymore.

将不再显示此错误。

for more details go herefor more details

欲了解更多详情,请点击此处了解更多详情

回答by Bugfixer

Start and end of your form

表格的开始和结束

{!!
    Form::
    open(
    array(
    'id'=>'FormIF',
    'name'=>'FormName',
    'autocomplete'=>'off' ,
    'url' =>route('route_to_post_method')
    )
    )
    !!}

//Form Body

{!! Form::close() !!}

回答by Mostafa Aabed

open your inspector and make sure the meta tag is placed in head . if you find meta placed in body , so you may include script out of section field in your section script, you must include into section field.

打开您的检查器并确保元标记放置在 head 中。如果您发现元放置在 body 中,因此您可以在部分脚本中包含部分字段之外的脚本,则必须包含到部分字段中。

@extends('app')
@section('content')
@include('sidebar')

回答by Abdelsalam Shahlol

All you have to do is to check your generated HTML file ctrl+U. You will notice that your HTML <head>tag is probably placed inside of the body tag. This happens if you don't get your layoutand bladesections right.

您所要做的就是检查您生成的 HTML 文件ctrl+U。您会注意到您的 HTML<head>标记可能位于 body 标记内。如果您的布局叶片部分不正确,就会发生这种情况。

This happened to me before

这发生在我身上

@extends('layouts.main-layout')
  <h1>Laravel Project</h1> <--This will break the **generated template**
@section('content')
    <div id="app"></div>
@endsection

The <h1>will break template you will see the CSRF-tokenerror in the console.

<h1>会打破模板,你会看到CSRF令牌在控制台中的错误。