Problems deleting via ajax with Laravel

Asked

Viewed 268 times

1

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

Man JavaScript is 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");   
        } 
    });
};

Man Controller thus:

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

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

My sheet of rotas is 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');

1 answer

3


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

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

In this particular case it is better to work with $.post of and also in the passage of the value always assign {'id' : id} to be recognized by the 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:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.