0
votes

I have a strange issue going on. I can't seem to get a record to delete via ajax submission. I'm not seeing anything wrong as this worked previously on Laravel 4. Do I need to provide the CSRF token to the method? If I change my route to any instead of post or delete and hit it directly it will delete as expected.

 {!! HTML::link(url(), 'Delete', array('class' => 'btn btn-delete', 'data-name' => $tile->tile_name, 'id' => $tile->id)) !!}

Ajax

 var id = $this.attr('id');

 // Submit delete request to route with id
 $.post('edit/delete/' + id);

 // Redirect to gallery
 window.location.href = 'http://ims-tiles.dev/';

Route

$router->post('edit/delete/{id}', [

    'as'    => 'tile.destroy',
    'uses'  => 'TileController@destroy'

]);

Destroy Method

public function destroy($id) {

    $tile = Tile::find($id);
    $tags = explode(' ', $tile->getTags());
    $tagIds = [];

    foreach($tags as $tag){

        array_push($tagIds, $tile->getTagId($tag));
    }

    $tile->tags()->detach($tagIds);
    $tile->delete();        

}
2
You're route listens to DELETE but you send a POST requestlukasgeiter
Hi! I even changed the route to list to a POST request and it still will not remove the record.Miura-shi
Do you get any errors in your browser console?lukasgeiter

2 Answers

1
votes

Do I need to provide the CSRF token to the method?

Yes, you do. Laravel's default CSRF protection applies to AJAX POST/PUT/DELETE/etc. requests just like they do non-AJAX ones.

0
votes

No worries. Thanks @ceejayoz! I was able to figure it out and it was pretty much do to the csrf token not being set in the headers for ajax requests. The following resources helped me for those interested.

http://laravel.com/docs/master/routing

http://words.weareloring.com/development/laravel/laravel-4-csrf-tokens-when-using-jquerys-ajax/