0

I am trying to delete a record using ajax from an alert button but nothing is done. Here is what i have so far The button

<button type="button" value="{{$announcement->id}}" id="close-alert" class="close" data-url="{{ route('member.postDeleteAnnouncement', ['id' => $announcement->id]) }}" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>

app.js

jQuery(document).ready(function($) {
$('#close-alert').click(function() {
    var url =$('#close-alert').data('url');
    ajax ({
        type: 'POST',
        url: url,
        success: function(){
                console.log('data sent');
            },
    });
});

});

and my controller action

public function postDeleteAnnouncement ($announcement_id)
{
    $user = \Sentinel::getUser();
    $member = $user->member;

    $member->announcements()->detach($announcement_id);
}

Any help is much appreciated.

here is the route code

Route::post('/announcement/{id}/delete', ['uses' => 'SiteController@postDeleteAnnouncement', 'as' => 'postDeleteAnnouncement']);
ahabsy
  • 85
  • 1
  • 1
  • 9

2 Answers2

0

Did u post the CSRF token? It this form or just button?

var _token = $("input[name='_token']").val();

and pass together with other data.

data: { _token:_token , etc:etc }

worth to try

ZZA
  • 727
  • 6
  • 6
  • It is just a button – ahabsy Jul 24 '18 at 07:21
  • sorry i thought ur's form , anyway read here u must pass CSRF token "https://stackoverflow.com/questions/32738763/laravel-csrf-token-mismatch-for-ajax-post-request/39530688" – ZZA Jul 24 '18 at 07:25
0

CSRF token is missing in your ajax request, and which is mandatory for every post type request. Use it like:

$.ajax({
...
method: 'post',
headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
...
});

and inside your blade view, use:

<meta name="csrf-token" content="{{ csrf_token() }}">

Reference

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59