does not send any kind of request when deleting record by ajax (jquery) and laravel

1

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?

    
asked by KurodoAkabane 30.11.2016 в 12:59
source

3 answers

1

It's because in your controller you have

public function destroy($id,Request $request)
{

    if($request->ajax()):
            $delCat = \App\modelos\categoria::find($request->$id);
            $delCat->delete();
    endif;

    return redirect()->to('/admin/categorias');

}

when in fact it is

public function destroy(Request $request, $id)
{

    if($request->ajax()):
            $delCat = \App\modelos\categoria::find($request->$id);
            $delCat->delete();
    endif;

    return redirect()->to('/admin/categorias');

}

link

    
answered by 03.01.2017 в 05:49
0

I think the form and the ajax function are in conflict. From my point of view you would not need to create the form, I would do something like this:

<input type="hidden" name='catid' value="{{ $cat->id }}" >
<input type="button" class="btn btn-danger glyphicon glyphicon-trash eliminarcategoria" value ="DEL" >
    
answered by 01.12.2016 в 17:33
-1

How about; -)

You should always develop looking at the logs to have everything checked in case of failure. Failures 500 is usually an error before or during the execution of Laravel, which does not reach the framework log, so if you are looking at it, you will not see the failure.

Suggestion, always review in this order:

1) apache access.log , to know if you entered your web server, with what parameters, if doing POST or GET, etc.

2) apache error.log , there you will see the bug at the web server level, many times you will see an error there and it will not be seen in the laravel log.

3) php_errors.log , if you have configured to send the errors from your php.ini

4) project / storage / logs / laravel.log, the log of your project generated by Laravel.

About your specific errors:

  • Error 500 : it may be a syntax fault, and you will not see it in the laravel log, so you have to review the previous logs.

  • Error 405 : You are trying to access a route in a different way than you defined it in laravel routes. And where you define that the route is post and in fact the ajax comes by get, it will fail you. It has to match exactly what is defined with what you are doing, check the apache access log and you will see if it enters and with what method (get or post).

  • Check these paths and you will see the concrete error where it is failing

    Anything you comment again.

    Greetings!

        
    answered by 02.12.2016 в 14:43