Sociopath
Sociopath

Reputation: 295

Deleting on laravel via confirmation message modal?

I want to delete something via jquery using my url on routes, I have this but ain't working dunno why

routes.php

Route::delete('/administrar/invitacion/{id}', 'invitacionController@eliminarInvitacion');

on my administrar.blade.php I have a select where i retrieve the id and this modal with two buttons

<!-- Modal borrar -->
                                                <div class="modal fade" id="borrar" role="dialog">
                                                    <div class="modal-dialog">
                                                        <!-- Modal content-->
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                                                <h4 class="modal-title">Borrar invitación</h4>
                                                            </div>
                                                            <div class="modal-body">
                                                                <p>¿Estás seguro que quieres cancelar esta invitación?</p>
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" id="delete_invite" class="btn btn-warning" data-dismiss="modal">Si</button>
                                                                <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>

invitacionController.php

public function eliminarInvitacion(Request $request, Invite $invite)
    {
        $this->authorize('destroy', $invite);

        $invite->delete();

        return redirect('/administrar');
    }

on my js:

  $('#borrar').on('click', '#delete_invite', function(e) {

        $id = $( "#selectinv option:selected" ).val();
        $.ajax({url: "/administrar/invitacion/"+$id, type: 'DELETE', success: function(result){


        }});
    })

Upvotes: 0

Views: 1127

Answers (2)

Sociopath
Sociopath

Reputation: 295

Deleting without a form, just had to add this before my button

<input type="hidden" id="token" name="_token" value="{{ csrf_token() }}">

then on js my example:

$('#borrar').on('click', '#delete_invite', function(e) {
    //Declaration
    var token = $('#token').val();
    var id = $( "#selectinv option:selected" ).val();

    $.ajaxSetup({
        headers: {
            'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
        }
    });

    //Delete request
    $.ajax({
        type:   'DELETE',
        url:    '/administrar/invitacion/' + id,
        data:   { _token :token },
        success: function(data){
             window.location="/administrar";
        }
    });

});

Upvotes: 1

Koeno
Koeno

Reputation: 1573

This does not work since you are using Route::deleteand you're not posting a CSRF token. To make this work, you either have to change your route to Route::get, or post a CSRF token.

To read more about this, see https://laravel.com/docs/master/routing#csrf-protection.

Also, if you have no idea why ajax calls are not working, open your console, then hit the "Network" tab, and then do your ajax request. The request will appear on the bottom, and then u can get details if you click on it.

Upvotes: 0

Related Questions