Felix Maxime Felix Maxime - 12 days ago 7
jQuery Question

AJAX post request not being processed in laravel

I have a "Like" feature on my laravel site. It works fine as a POST request, however I'd like to use AJAX for the request as to not have to reload the page. I gave it an attempt, however I haven't done much AJAX in the past, and I'm getting some errors.

Markup:

<li data-value="{{$status->id}}" class="like">
Like
</li>


Laravel route:

Route::get('/status/{statusId}/like', [
'uses' => '\CommendMe\Http\Controllers\StatusController@getLike',
'as' => 'status.like',
'middleware' => ['auth'],
]);


JS:

$('.like').click(function() {
$likeValue = $(this).attr('data-value');
$token = $('meta[name=csrf-token]').attr("content");
$.ajax({
type: "POST",
url: host + '/status/' + $likeValue + '/like',
data: {likeValue: $likeValue, _token:$token},
}).success(function (res){

});
});


This will return the error:


TypeError: $.ajax(...).success is not a function


Help!

Answer

I see why this wasn't working. My route was expecting a GET request, not a POST. D'oh!