ajax post in laravel 5 return error 500 (Internal Server Error)

后端 未结 12 799
不知归路
不知归路 2020-11-29 05:13

this is my test ajax in laravel 5 (refer below)

$(\"#try\").click(function(){
    var url = $(this).attr(\"data-link\");
    $.ajax({
        url: \"test\",
         


        
相关标签:
12条回答
  • 2020-11-29 05:42

    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;
    
    0 讨论(0)
  • 2020-11-29 05:45

    90% of the laravel ajax internal server error is due to missing CSRF token. other reasons can inlucde:

    • Wrong Request Type (e.g sending post to get)
    • Wrong data type recived (e.g ajax is expecting JSON and app returns string)
    • Your .htaccess is misconfigured
    • Missing Route
    • Code Error

    You can read further about this in details here: https://abbasharoon.me/how-to-fix-laravel-ajax-500-internal-server-error/

    0 讨论(0)
  • 2020-11-29 05:46

    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
    });
    
    0 讨论(0)
  • 2020-11-29 05:48

    By default Laravel comes with CSRF middleware.

    You have 2 options:

    1. Send token in you request
    2. Disable CSRF middleware (not recomended): in app\Http\Kernel.php remove VerifyCsrfToken from $middleware array
    0 讨论(0)
  • 2020-11-29 05:56

    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.

    0 讨论(0)
  • 2020-11-29 05:56

    do not forget add "use Illuminate\Http\Request;" on your controller

    0 讨论(0)
提交回复
热议问题