POST 419 (unknown status) laravel error

2019-08-26 02:06发布

i want to submit my form using ajax following is my 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/submit 419 (unknown status)

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)

3条回答
祖国的老花朵
2楼-- · 2019-08-26 02:57

1) Use the meta tag in head section

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

2) Set header in ajax , like

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

3) Send CSRF token with data

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

or CSRF token can be written as

 "_token": "{{ csrf_token() }}",
查看更多
放荡不羁爱自由
3楼-- · 2019-08-26 03:07

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") }
});
查看更多
Juvenile、少年°
4楼-- · 2019-08-26 03:12

You can add below code to your master file

<script>
    $.ajaxSetup({
        headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
    });
</script>
查看更多
登录 后发表回答