this is my test ajax in laravel 5 (refer below)
$(\"#try\").click(function(){
var url = $(this).attr(\"data-link\");
$.ajax({
url: \"test\",
Short and Simple Solution
e.preventDefault();
var value = $('#id').val();
var id = $('#some_id').val();
url="{{url('office/service/requirement/rule_delete/')}}" +"/"+ id;
console.log(url);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
/* the route pointing to the post function */
url: url,
type: 'DELETE',
/* send the csrf-token and the input to the controller */
data: {message:value},
dataType: 'JSON',
/* remind that 'data' is the response of the AjaxController */
success: function (data) {
console.log(data)
//$('.writeinfo').append(data.msg);
//$('#ruleRow'+id).remove();
}
});
return false;
90% of the laravel ajax internal server error is due to missing CSRF token. other reasons can inlucde:
You can read further about this in details here: https://abbasharoon.me/how-to-fix-laravel-ajax-500-internal-server-error/
I guess this has been solved by now but still the best thing to do here is to send the token with your form
{!! csrf_field() !!}
and then in your ajax
$("#try").click(function(){
var url = $(this).attr("data-link");
$.ajax({
url: "test",
type:"POST",
data: { '_token': token, 'someOtherData': someOtherData },
success:function(data){
alert(data);
},error:function(){
alert("error!!!!");
}
}); //end of ajax
});
By default Laravel comes with CSRF middleware.
You have 2 options:
for me this error cause of different stuff. i have two ajax call in my page. first one for save comment and another one for save like. in my routes.php i had this:
Route::post('posts/show','PostController@save_comment');
Route::post('posts/show','PostController@save_like');
and i got 500 internal server error for my save like ajax call. so i change second line http request type to PUT and error goes away. you can use PATCH too. maybe it helps.
do not forget add "use Illuminate\Http\Request;" on your controller