POST请求不能使用Ajax laravel 4(投票系统)


POST Request not working using Ajax laravel 4 (voting system)

我正在实现一个投票系统。我试图在laravel 4实现AJAX,但我有一个500内部服务器问题。AJAX代码可以创建用户系统。

我的形式:

{{ Form::open(array('url' => 'profilepublicationvote', 'class' => 'vote_ajax')) }}
<input type="text" id="disabledTextInput" style="display:none" name="vote" value='1'>
<input type="text" id="disabledTextInput" style="display:none" name="user_id" value="{{ Auth::user()->id }}">
<input type="text" id="disabledTextInput" style="display:none" name="publication_id" value="{{ $publication->id }}">
{{ Form::submit('vote', array('class' => 'button expand round')) }}
{{ Form::close() }}

routes.php

Route::post('profilepublicationvote', function(){
if(Request::ajax()){
    $votedata = array(
        'vote'          =>  Input::get('vote'),
        'user_id'       =>  Input::get('user_id'),
        'publication_id'=>  Input::get('publication_id'),
    );
        $vote = new Profilepublicationvote($votedata);
        $vote->save(); 
        if($user)
        {
            return Response::json(array(
                'success'         =>     true,
                'message'         =>     'Yeah!'
            ));
        }
}
});

我的javascript:

 $(document).ready(function() {
var form = $('.vote_ajax');
    form.bind('submit',function () {
        $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            data: form.serialize(),
            complete: function(data){
            },
            success: function (data) {
                $('.success_message').hide().html('');
                    $(form)[0].reset();
                    $('.success_message').show().html(data.message)
            },
        });
   return false;
});
});

edit1:我已经改变$user $vote,但这不是问题。

你没有$user变量,我认为你的if语句总是false

也许你应该这样做

    if($vote->save())

我已经解决了我的问题改变我的路线:

Route::post('profilepublicationvote', function(){
if(Request::ajax()){
        $voto = new Profilepublicationvote();
        $voto->vote         =   Input::get('vote');
        $voto->user_id          =  Input::get('user_id');
        $voto->publication_id   =  Input::get('publication_id');
        $voto->save(); 
}
});