vendredi 25 novembre 2016

Ajax call with Laravel 5 returns Error 500 (Internal Server Error), even when csrf token is passed

The title say's it all.

.blade.php file:

@foreach($posts as $post)

            <article class="post" data-postid="  ">
                <p></p>
                <div class="info"> 
                    Posted by   on 
                </div>
                <div class="interaction">
                    <a href="#" class="like">Like</a> |
                    <a href="#" class="like">Dislike</a>
                    @if(Auth::user() == $post->user) 
                        |
                        <a href="#" class="edit">Edit</a> |
                        <a href="">Delete</a> 
                    @endif
                </div>
            </article>

        @endforeach

JS within .blade.php file:

<script>
        var token = '';
        var urlEdit = '';
        var urlLike = '';
    </script>

.js file:

 $('.like').on('click', function(event){
    event.preventDefault();
    postId = event.target.parentNode.parentNode.dataset['postid'];
    var isLike = event.target.previousElementSibling == null; //Checks if it's a like or dislike.

    $.ajax({
        method: 'POST',
        url: urlLike,
        data: {isLike: isLike, postId: postId, _token: token}
    })
    .done(function(){

    });
});

  • I've tried to check if I did something wrong with the route.
  • I tried passing the token inside a meta-tag instead of defining it as a javascript variable
  • I tried using double bracelets " instead of single ones '.

Nothing seems to work, what's going on here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire