A
A
adam_carraway2019-06-28 16:18:04
Laravel
adam_carraway, 2019-06-28 16:18:04

What causes the 419 (unknown status) error?

I send a post request through ajax, as a result, an error 419 . (if get request then no errors)

$.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: "/exit",
                    type: "POST",
                    data: "get=on",
                    success: function (response) {
                       console.log('+')
                    }
                });

is present in the hat
<meta name="csrf-token" content="{{ csrf_token() }}">

Router Route::post('/exit', '[email protected]');
How to solve this error

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
adam_carraway, 2019-06-28
@adam_carraway

Solved the problem by passing the token to data

$.ajax({
                    url: "/exit",
                    type: "POST",
                    data: {"_token": $('meta[name="csrf-token"]').attr('content')},
                    success: function (response) {
                       console.log('+')
                    }
                });

E
empirikk, 2022-03-07
@empirikk

I have a working scheme, without form it looks like this, without going into details, like this:
HTML:

<div>
@csrf{{--эта конструкция преобразуется в input c именем _token, пример: <input type="hidden" name="_token" value="tyiV5lsNJy9aA245dke7979aW1otyutV5D">--}}
<input class='text' type='text'>
<input type='submit'>
</div>

jQuery:
let someV = $('.text').val();
let token = $("input[name='_token']").val();
                $.ajax({
                    type: 'POST',
                    url: '/someurl',
                    data: {
                       '_token': token,
                        "someV" : someV
                    },
                    success:function (res){
                        console.log('F**k yeah');
                    }

                })

Hope it helps someone)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question