POST 419(未知状态)laravel 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49306385/
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:31:10  来源:igfitidea点击:

POST 419 (unknown status) laravel error

phpajaxlaravellaravel-5

提问by Pranav Mandlik

i want to submit my form using ajax following is my javascript

我想使用ajax提交我的表单以下是我的javascript

$.ajax({

      type: "POST",
      headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
      url: "http://localhost/shago/register/submit",
      data: user_firstname,

      dataType: "text",
      success: function(resultData) { alert("Save Complete") }
});

i have included meta tag in form like

我已经以类似的形式包含了元标记

<div id="individual" class="hid">
<form method="POST" id="individual_form" name="individual_form" action="{{ route('register.submit') }}">
<meta name="csrf-token" content="{{ csrf_token() }}">

and in controller i have just returned a message but i am getting

在控制器中,我刚刚返回了一条消息,但我收到了

POST http://localhost/shago/register/submit419 (unknown status)

POST http://localhost/shago/register/submit419(未知状态)

above error can you please help me ,let me know for any other inputs i know it is mostly caused by csrf token (i have declared submit route in web.php and also api.php file)

以上错误能否请您帮帮我,让我知道任何其他输入,我知道这主要是由 csrf 令牌引起的(我已经在 web.php 和 api.php 文件中声明了提交路由)

回答by Sérgio Reis

Try this

尝试这个

$.ajax({
  type: "POST",
  headers: {
  'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
  url: "http://localhost/shago/register/submit",
  data: {// change data to this object
     _token : $('meta[name="csrf-token"]').attr('content'), 
     user_firstname:user_firstname
  }
  dataType: "text",
  success: function(resultData) { alert("Save Complete") }
});

回答by John

You can add below code to your master file

您可以将以下代码添加到您的主文件中

<script>
    $.ajaxSetup({
        headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
    });
</script>

回答by Murad

1) Use the meta tag in head section

1)在head部分使用meta标签

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

2) Set header in ajax , like

2)在 ajax 中设置标题,比如

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

3) Send CSRF token with data

3)发送带有数据的CSRF令牌

     data:({_token : $('meta[name="csrf-token"]').attr('content'), 
name:name,category:category}),

or CSRF token can be written as

或 CSRF 令牌可以写为

 "_token": "{{ csrf_token() }}",