phpajaxlaravellaravel-5

Laravel 5 Ajax Delete not working


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();        

}

Solution

  • Thanks to @ceejayoz, I was able to figure it out and it was pretty much due 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/