Не получается удалить запись из БД используя Ajax. CRUD без Ajax работает.
@extends('master')
@section('content')
@include('errors')
<div class="pull-right">
<a href="{{route('item.create')}}" class="btn btn-info">Add new</a>
</div>
<div class=" table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>N</th>
<th>Name</th>
<th>Parent_id</th>
<th>Show Edit Delete</th>
</tr>
</thead>
@foreach($items as $item)
<tbody>
<th>{{$item->id}}</th>
<th>{{$item->name}}</th>
<th>{{$item->parent_id}}</th>
<th>
{{Form::open()}}
{{Form::button('Delete',['type'=>'submit','class'=>'btn btn-block btn-danger send'])}}
{{Form::close()}}
</th>
</tbody>
@endforeach
</table>
</div>
<p><b></b>
</p>
<script src="{{asset('js/delAjax.js')}}"></script>
@endsection
Код delAjax.js (в базе есть запись с id 13)
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.send').on('submit',function (e) {
e.preventDefault();
$.ajax({
type: 'delete',
url:'{{route("ajax"}}',
data:{id:13},
success:function (data) {
console.log(data)
}
})
})
})
Контролер
public function ajax($id)
{
Test::findOrFail($id)->delete();
return response()->json(['success'=>'Item deleted']);
}
Маршрут
Route::delete('/ajax/','MainController@ajax')->name('ajax');
Консоль выдает
CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token
(anonymous) @ app.js:13953
__webpack_require__ @ app.js:20
(anonymous) @ app.js:13896
__webpack_require__ @ app.js:20
(anonymous) @ app.js:13881
__webpack_require__ @ app.js:20
(anonymous) @ app.js:63
(anonymous) @ app.js:66
app.js:36551 [Vue warn]: Cannot find element: #app
Хотя вроде бы токены есть.
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="http://192.168.10.10:2222/js/app.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css"/>
<meta name="csrf-token" content="Y0UqBwRALqmRw3dfrKEuKoz4gURVkl0tHxvacRJx">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="http://192.168.10.10:2222/css/app.css">
<link rel="stylesheet" href="http://192.168.10.10:2222/css/tree.css">
<!-- Optional JavaScript for treview-->
<link rel="stylesheet" href="http://192.168.10.10:2222/css/bootstrap-treeview.min.cs">
<title>BlackSeaCorporation</title>
<!-- token -->
<meta name="csrf-token" content="Y0UqBwRALqmRw3dfrKEuKoz4gURVkl0tHxvacRJx">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light ">
<a class="navbar-brand" href="/">B_S_C</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse nav-fill" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item ">
<a class="nav-link" href="/table">Admin table <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
Sigh in
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Add Item</a>
<a class="dropdown-item" href="#">Add with AJAX</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Show all</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0" action="http://192.168.10.10:2222/search">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name="s">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<div class="container">
<div class="pull-right">
<a href="http://192.168.10.10:2222/item/create" class="btn btn-info">Add new</a>
</div>
<div class=" table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>N</th>
<th>Name</th>
<th>Parent_id</th>
<th>Show Edit Delete</th>
</tr>
</thead>
<tbody>
<th>5</th>
<th>AudiQ7</th>
<th>3</th>
<th>
<form method="POST" action="http://192.168.10.10:2222/index1" accept-charset="UTF-8"><input name="_token" type="hidden" value="Y0UqBwRALqmRw3dfrKEuKoz4gURVkl0tHxvacRJx">
<button type="submit" class="btn btn-block btn-danger send">Send</button>
</form>
</th>
</tbody>
<tbody>
<th>14</th>
<th>Samsung</th>
<th>2</th>
<th>
<form method="POST" action="http://192.168.10.10:2222/index1" accept-charset="UTF-8"><input name="_token" type="hidden" value="Y0UqBwRALqmRw3dfrKEuKoz4gURVkl0tHxvacRJx">
<button type="submit" class="btn btn-block btn-danger send">Send</button>
</form>
</th>
</tbody>
<tbody>
<th>15</th>
<th>Audi</th>
<th>1</th>
<th>
<form method="POST" action="http://192.168.10.10:2222/index1" accept-charset="UTF-8"><input name="_token" type="hidden" value="Y0UqBwRALqmRw3dfrKEuKoz4gURVkl0tHxvacRJx">
<button type="submit" class="btn btn-block btn-danger send">Send</button>
</form>
</th>
</tbody>
<tbody>
<th>17</th>
<th>Yuriy Peresypko</th>
<th>3</th>
<th>
<form method="POST" action="http://192.168.10.10:2222/index1" accept-charset="UTF-8"><input name="_token" type="hidden" value="Y0UqBwRALqmRw3dfrKEuKoz4gURVkl0tHxvacRJx">
<button type="submit" class="btn btn-block btn-danger send">Send</button>
</form>
</th>
</tbody>
<tbody>
<th>18</th>
<th>Number1</th>
<th>1</th>
<th>
<form method="POST" action="http://192.168.10.10:2222/index1" accept-charset="UTF-8"><input name="_token" type="hidden" value="Y0UqBwRALqmRw3dfrKEuKoz4gURVkl0tHxvacRJx">
<button type="submit" class="btn btn-block btn-danger send">Send</button>
</form>
</th>
</tbody>
</table>
</div>
<p><b></b>
</p>
<script src="http://192.168.10.10:2222/js/delAjax.js"></script>
<div id="treeview"></div>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossorigin="anonymous"></script>
</body>
</html>