Patrik Horváth
Patrik Horváth

Reputation: 143

Laravel ajax Delete request - unknow status

Hi i cant figure out why my ajax call does return Unknow status error (419), because ajax call looks good also route name is good and Card import is added also =\

{{}} is here cause its inside Blade engine

here is my AJAX call

       $.ajax({
            type: "delete",
            url: "{{route("cart.destroy", $cartitemjs->rowId)}}",
            success: function () {
                console.log("Produkt bol vymazani");
            }
        })

and there is my destroy mehod inside a controller

public function destroy($id)
{
    Cart::remove($id);
}

DELETE | cart/{cart} | cart.destroy | App\Http\Controllers\CartController@destroy | web

Upvotes: 1

Views: 547

Answers (1)

Maraboc
Maraboc

Reputation: 11083

if you want to use the web section you should add X-CSRF-TOKEN header for your ajax calls with the value of the generated csrf_token that you can get it from the meta tag for example like this :

<meta name="_token" content="{{ csrf_token() }}"/>

Then use $.ajaxSetup before ajax call:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
    }
});

$.ajax({
    type: "delete",
    // ....
})

Upvotes: 2

Related Questions