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

后端 未结 12 800
不知归路
不知归路 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:57

    Laravel 7.X In bootstrap.js, in axios related code, add:

    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = $('meta[name="csrf-token"]').attr('content');
    

    Solved lot of unexplained 500 ajax errors. Of course it's for those who use axios

    0 讨论(0)
  • 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): <meta name="csrf-token" content="{{ csrf_token() }}">

    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.

    0 讨论(0)
  • 2020-11-29 06:04

    You can add your URLs to VerifyCsrfToken.php middleware. The URLs will be excluded from CSRF verification.

    protected $except = [
        "your url",
        "your url/abc"
    ];
    
    0 讨论(0)
  • 2020-11-29 06:05

    In App\Http\Middleware\VerifyCsrfToken.php you could try updating the file to something like:

    class VerifyCsrfToken extends BaseVerifier {
    
        private $openRoutes =
        [
            ...excluded routes
        ];
    
        public function handle($request, Closure $next)
        {
            foreach($this->openRoutes as $route)
            {
                if ($request->is($route))
                {
                    return $next($request);
                }
            }
    
            return parent::handle($request, $next);
        }
    };
    

    This allows you to explicitly bypass specific routes that you do not want verified without disabling csrf validation globally.

    0 讨论(0)
  • 2020-11-29 06:06

    you have to pass the csrf field through ajax please look at the code here

    $.ajax({
                                            type: "POST",
                                            url:'{{URL::to("/delete-specialist")}}',
                                            data: {
                                                id: id,
    
                                                _token: $('#signup-token').val()
                                            },
                                            datatype: 'html',
                                            success: function (response) {
                                                if(response=="deleted"){
                                                    $("#"+id).hide();
                                                    $("#message").html("successfully deleted");
                                                }
    
                                            }
    
                                        });
    

    and you also need to write this input field before this

    <input id="signup-token" name="_token" type="hidden" value="{{csrf_token()}}">
    

    still if you do not understand please enjoy this video https://www.youtube.com/watch?v=ykXL8o0slJA&t=20s

    0 讨论(0)
  • 2020-11-29 06:06

    Using post jquery instead helped me to solve this problem

    $.post('url', data, function(response) {
        console.log(response);
    });
    
    0 讨论(0)
提交回复
热议问题