I've been trying to solve this for a few days and I do not get it, I am doing the logic to delete by ajax records but it does not work for me, if I go to the section of firefox for developers, in the network tab, once I make the request, it does not do anything, it does not send any kind of POST request, I tried it is well linked and effectively works jquery on that page, but with ajax does not just work, I think it can be two things, one is the route
url: 'http://localhost/ficheros/laravel_escuelait/public/admin/categorias/'+valorIdCategoria,
that works perfectly, that is to say valueIdCategoria takes the value that it has to take and it is the route that has to be sent in the "delete" requests.
The other factor that I think may fail is the data, that is
data: {'_method':'DELETE','id': valorIdCategoria},
If I try it this way, errorThrown of the fail method throws this at me
error 500 _ Internal Server Error
However, if I put it that way
data: {id: valorIdCategoria}
, or so
data: {'id': valorIdCategoria},
I get this error
error 405 _ Method Not Allowed
I put the rest of the code
CONTROLLER CATEGORIES (NEVER ARRIVES TO THE CONTROLLER)
public function destroy($id,Request $request)
{
if($request->ajax()):
$delCat = \App\modelos\categoria::find($request->$id);
$delCat->delete();
endif;
return redirect()->to('/admin/categorias');
}
CODE OF VIEW
<table class="table table-bordered table-hover" id="listadopost">
<thead>
<tr class="text-uppercase">
<th>Categorias</th>
<th>Editar</th>
<th>Eliminar</th>
</tr>
</thead>
<tbody>
@foreach($listacat as $cat)
<tr>
<td class="text-capitalize nombrecatx">{{$cat->nombrecat}}</td>
<td><a href="{{route('admin.categorias.edit',array($cat->id))}}" class="btn btn-warning"><span class="glyphicon glyphicon-edit"></span></a></td>
<td>
{!! Form::open(array('route' =>array('admin.categorias.destroy',$cat->id),'method'=>'DELETE')) !!}
{!! Form::hidden('catid', $cat->id, []) !!}
<input type="submit" class="btn btn-danger glyphicon glyphicon-trash eliminarcategoria" value ="DEL" >
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
THE ROUTES
Route::group(['prefix' => 'admin','middleware' => 'auth'], function() {
Route::resource('categorias','categoriaController');
});
THE AJAX FUNCTION, CHARGED AT THE SIGHT
$(document).ready(function() {
$('.eliminarcategoria',this).click(function(e){
e.preventDefault();
var fila = $(this).parents('tr');
var valorIdCategoria = fila.find('input[name="catid"]').val();
var valorCategoria = String(fila.find('td.nombrecatx').text());
var color = fila.css({'background-color':'maroon',
'font-size':'1.5em',
'color':'white',
});
$.ajax({
url: 'http://localhost/ficheros/laravel_escuelait/public/admin/categorias/'+valorIdCategoria,
method: 'POST',
dataType: 'json',
//data: {'id': valorIdCategoria},
data: {'_method':'DELETE','id': valorIdCategoria},
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') },
})
.done(function(data) {
console.log("success");
$('#panelposts').before(notificacion('success','El registro se ha eliminado correctamente desde ajax'));
})
.fail(function(ex, errorThrown) {
console.log("error "+ex.status+' _ '+errorThrown);
$('#panelposts').before(notificacion('danger','ERROR FATAL'+errorThrown));
})
.always(function() {
console.log("complete");
});
})
});
I do not really know what is the reason why he does not send any kind of request, in other projects I've done using ajax although I could give some kind of error at least send the POST request, erronea that if, but the He sent.
Any idea what it can be?