Php 使用Laravel的简单AJAX投票系统

Php 使用Laravel的简单AJAX投票系统,php,jquery,ajax,laravel,polling,Php,Jquery,Ajax,Laravel,Polling,我正在创建一个简单的拉威尔投票脚本,到目前为止还不错。。。但是,在不重新加载页面的情况下呈现适当的视图时,我会陷入困境。这是一个简单的问题,但我无法让它工作: 这是我的路线/管制员代码: Route::post('poll/vote', function() { $poll = Poll::findOrFail(Input::get('id')); $pollOption = PollOption::findOrFail(Input::get('option_id'));

我正在创建一个简单的拉威尔投票脚本,到目前为止还不错。。。但是,在不重新加载页面的情况下呈现适当的视图时,我会陷入困境。这是一个简单的问题,但我无法让它工作:

这是我的路线/管制员代码:

Route::post('poll/vote', function() {
    $poll = Poll::findOrFail(Input::get('id'));

    $pollOption = PollOption::findOrFail(Input::get('option_id'));
    $pollOption->increment('votes');

    $cookie = Cookie::make('poll_vote_' . $poll->id, true); //User voted on this poll, create a cookie
    $view = View::make('includes.poll', ['poll' => $poll])->render();

    $response = Response::json(['view' => $view]);
    $response->headers->setCookie($cookie);
    return $response;
});
jQuery AJAX并不重要,因为它可以完美地工作:

$('.poll-option').click(function(e) {
    e.preventDefault();
    var form = $(this).parents('form'),
        url = form.attr('action');

    $.ajax({
        type: 'POST',
        url: url,
        data: form.serialize(),
        dataType: 'json'
    }).done(function(view) {
        $('#poll .content').html(view['poll']).find('.loading').remove();
    }).fail(function() {
        alert('Er is iets fout gegaan tijdens het antwoorden van de poll');
    });
});
还有我的观点

<div class="question">{{{ $poll->question }}}</div>
@if(Cookie::has('poll_vote_' . $poll->id))
    <ul class="list small votes">
    @foreach($poll->options as $option)
        <li>
            <span class="light">{{ $option->getVotePercentage() }}</span> {{{ $option->title }}}
            <div class="percentage-bar-container">
                <div class="percentage-bar" style="width: {{ $option->getVotePercentage() }}"></div>
            </div>
        </li>
    @endforeach
    </ul>
@else
    <ul class="list small options">
    {{ Form::open(['url' => 'poll/vote']) }}
        {{ Form::hidden('id', $poll->id) }}
        @foreach($poll->options as $option)
            <li>{{ Form::radio('option_id', $option->id, null, ['class' => 'poll-option']) }} {{{ $option->title }}}</li>
        @endforeach
    {{ Form::close() }}
    </ul>
@endif
在视图中,我检查cookie是否已设置。如果是,则显示投票结果,否则显示可投票的选项。现在,如果用户对某个选项进行投票,视图仍会显示选项,因为页面未刷新,所以Cookie尚未设置。如何解决此问题或创建解决方案

提前谢谢