douqie6454 2016-02-23 23:33
浏览 47
已采纳

关于AJAX的更新号,laravel 5.2

Basically what is happening is when I press the button then I get a +1 point on post. Everything is working excellent but I don't know how to update the number itself without page refresh. The info is already send to database I just don't know how to change number without page refresh.. This is my jQuery:

$(document).ready(function(){
    $('form').submit(function(evt){
        evt.preventDefault();
        var url = $(this).attr("action");
        var formData = $(this).serialize();
        $.get(url, formData, function(response){

        });
    });
});

And my html:

<p>Not recommended: <strong><span id="votes" class="label label-success">{{$s->points}}</span></strong></p>
  • 写回答

2条回答 默认 最新

  • doujiena0025 2016-02-23 23:40
    关注

    On the client side

    $(document).ready(function(){
        $('form').submit(function(evt){
            evt.preventDefault();
            var url = $(this).attr("action");
            var formData = $(this).serialize();
            $.get(url, formData, function(response){
                // This function runs when you successfully return
                $('#votes').html( response.newNumber );
            });
        });
    });
    

    And on the Laravel side

    public function getVote(Request $request) {
        // Process form
        $points = 5;
        return response()->json(['newNumber' => $points]);
    }
    

    More on $.get jQuery function

    Also, I don't know the nature of your app, but it's recommended that if you're making changes to data on the server side (i.e. upvoting a post) you use a POST request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?