Problems deleting via ajax with laravel

1

I'm having trouble effecting a deletion with Laravel 5.3 . By submitting the ajax it simply is not returning anything to me.

My JavaScript looks like this:

$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });

function excluir(id){
    swal({   
        title: "Tem certeza?",   
        text: "Você não será capaz de recuperar este item!",   
        type: "warning",   
        showCancelButton: true,   
        confirmButtonColor: "#DD6B55",   
        confirmButtonText: "Sim, excluir!",   
        cancelButtonText: "Não, voltar!",   
        closeOnConfirm: false,   
        closeOnCancel: false 
    }, function(isConfirm){   
        if (isConfirm) {
             var url = location.href; //pega endereço que esta no navegador
            url = url.split("/"); //quebra o endeço de acordo com a / (barra)

            jQuery.ajax({
                type: "POST",
                url: url[2] + "/clientes/delete",
                data: id,
                success: function( data )
                {
                    alert( data );
                    swal("Excluido!", "Seu item foi excluido com sucesso. "+id, "success"); 

                }
            });
        } else {     
            swal("Cancelado", "Seu item está seguro", "error");   
        } 
    });
};

My Controller like this:

public function delete(\Illuminate\Http\Request $request){
    $id = $request->input('id');
    return $id;
}

But nothing happens, I tried to use an app to send a POST and check the return, and the error that he presented me was due to CSRF_TOKEN but I added the first line in my javascript and it did not work.

My rotas sheet looks like this:

Route::singularResourceParameters();

Route::auth();

// ROTAS DE DELEÇÃO

Route::post('/clientes/delete', [
    'uses' => 'ClienteController@delete',
    'as' => 'produtos.delete'
    ]);

// FIM DE ROTAS DE DELEÇÃO

Route::get('/', function () {
    return view('welcome');
});

Route::resource('clientes', 'ClienteController');
    
asked by anonymous 08.12.2016 / 23:25

1 answer

3

There are configuration errors that compromise the submission of the request via no . $.ajaxSetup was wrong with the headers key as described in the documentation itself of the framework .

$.ajaxSetup({
     headers: {
         'X-CSRF-TOKEN': '{{ csrf_token() }}'
     }
});

In this specific case, it is better to work with $.post of {'id' : id} to be acknowledged by Request :

function excluir(id)
{
    swal({   
        title: "Tem certeza?",   
        text: "Você não será capaz de recuperar este item!",   
        type: "warning",   
        showCancelButton: true,   
        confirmButtonColor: "#DD6B55",   
        confirmButtonText: "Sim, excluir!",   
        cancelButtonText: "Não, voltar!",   
        closeOnConfirm: false,   
        closeOnCancel: false 
    }, function(isConfirm){ 

        if (isConfirm) 
        {
            $.post('{{route("produtos.delete")}}', {'id':id}, function (result)
            {
               alert(result); 
               swal("Excluido!", 
                    "Seu item foi excluido com sucesso. " + result,
                    "success");            
            });             
        } 
        else 
        {     
            swal("Cancelado", "Seu item está seguro", "error");   
        }
    });
};

References:

08.12.2016 / 23:40