dimanche 6 mars 2016

create LIKE button using AJAX - laravel 5

I want create a like button for posts but when i try it i get this error:

TokenMismatchException in VerifyCsrfToken.php line 46:

    in VerifyCsrfToken.php line 46
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
    at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
    at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
    at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
    at Pipeline->then(object(Closure)) in Kernel.php line 115
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
    at Kernel->handle(object(Request)) in index.php line 53
    at require_once('C:\wamp\www\forum\public\index.php') in server.php line 21

And this is my form:

<form method="POST" id="post_up" action="">
    <input type="hidden" name="post"  value="{{ $post->id }}" />
    <input type="hidden" name="user"  value="{{ Auth::user()->id }}" />
    <button type="submit" class="btn btn-default" aria-label="Left Align">
        <span class="glyphicon glyphicon-menu-up" aria-hidden="true"></span>
    </button>
</form>

jQuery code:

<script type="text/javascript" src="{{ asset('js/jquery-2.2.1.js')}}"></script>
<script type="text/javascript">
    $(function(){
        $(#errors_).hide();
        $(#post_up).submit(function(e){
                e.preventDefault();
                var post = $('input[name="post"]').val();
                var user = $('input[name="user"]').val();
                var data = new FormData();
                data.append('user',user);
                data.append('post',post);
                $.ajax({
                    url:'post/post_vote_up',
                    type:'POST',
                    data:data,
                    contentType:"multipart/form-data",
                    processData:false,
                    success:function(data){alert('Section created :)')},
                    error:function(data){
                        $(#errors_).show();
                        $(#errors_).html('');
                        var errors = data.responseJSON;
                        $.each(errors,function(k,v){
                            $(#errors_).append(v+'<br>');
                        })
                    }
                });
            }
        })
    })
</script>

Controller function:

public function post_up(Request $r)
{   
    $p_id = $r->input('post');
    $u_id = $r->input('user');
    $post = new \App\Post_vote;
    $post->post_id=$p_id;
    $post->user_id=$u_id;
    $post->vote=1;
    $post->save();
}

route via this function:

Route::post('/post/post_vote_up','PostController@post_up');



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1QA5LzK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire