Я пытаюсь удалить строку в db с помощью Jquery/Ajax.DELETE с AJAX (JQuery) не работает с Laravel 5.2
Я следил за this thread, но я не могу заставить его работать.
Вот мое мнение:
@foreach($tournaments as $tournament)
<tr id="line" data-id="{{$tournament->id}}">
<td>
<a href="{!! URL::action('[email protected]', $tournament->id) !!}">{{ $tournament->id }}</a>
</td>
<td>{{ $tournament->owner->name}}</td>
<td class="text-center">
{!! Form::open(['method' => 'DELETE', 'id' => 'formDeleteTourament', 'action' => ['[email protected]', $tournament->id]]) !!}
{!! Form::button('<i class="glyphicon glyphicon-remove"></i>', ['class' => 'btn text-warning-600 btn-flat btnDeleteTournament', 'data-id' => $tournament->id ]) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
<script>
$(function() {
$('#.tnDeleteTournament').on('click', function (e) {
var inputData = $('#formDeleteTourament').serialize();
var dataId = $('.btnDeleteTournament').attr('data-id');
console.log(inputData); // displays: _method=DELETE&_token=tGFhaAr5fVhDXEYL3SaXem3WaTNJlSdFEkaVDe9F
console.log(dataId); // displays 1
var $tr = $(this).closest('tr');
swal({
title: "Are you sure?",
text: "You will not be able to recover this Tournament!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#EF5350",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel pls!",
closeOnConfirm: false,
closeOnCancel: false
},
function (isConfirm) {
if (isConfirm) {
e.preventDefault();
$.ajax(
{
type: 'POST', // I tried with DELETE too
url: '{{ url('/tournaments/') }}' + dataId,
data: inputData,
success: function (msg) {
console.log('success'); // It triggers!
$tr.find('td').fadeOut(1000, function() {
$tr.remove();
});
swal({
title: "Deleted!",
text: "Tournament has been deleted.",
confirmButtonColor: "#66BB6A",
type: "success"
});
},
error: function() {
swal("Oops", "We couldn't connect to the server!", "error");
}
}
)
}
else {
swal({
title: "Cancelled",
text: "Your Tournament is safe :)",
confirmButtonColor: "#2196F3",
type: "error"
});
}
});
});
});
Вот мой Контроллер илит:
public function destroy(Tournament $tournament)
{
$tournament->delete();
return response(['msg' => 'Product deleted', 'status' => 'success']);
}
Раньше у меня было работать без AJAX поэтому маршрут OK.
все работает отлично (модальности шоу), но не удаляет мой турнир
Я новичок в JS/JQuery, так что я не знаю, что происходит.
Любая идея, как исправить это?
Наверно потому, что у вас есть идентификатор, который повторяется. Идентификационные значения должны быть уникальными, иначе только первый экземпляр будет работать правильно. – Rasclatt
aaaah ok. Как его исправить? –
Вы можете автоинкрементять свои идентификаторы для одного или использовать класс вместо селектора. – Rasclatt