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

后端 未结 12 1064
不知归路
不知归路 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 06:01

    While this question exists for a while, but no accepted answer is given I'd like to point you towards the solution. Because you're sending with ajax, and presumably still use the CSRF middleware, you need to provide an additional header with your request.

    Add a meta-tag to each page (or master layout):

    And add to your javascript-file (or section within the page):

    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });
    

    See https://laravel.com/docs/master/csrf#csrf-x-csrf-token for more details.

提交回复
热议问题