简体   繁体   中英

POST 419 (unknown status) laravel error

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)

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

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() }}",

You can add below code to your master file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM