Laravel 5:如何分析Ajax响应

时间:2015-07-07 12:11:06

标签: php jquery ajax pagination laravel-5

我正在进行搜索并基于此我通过Jquery-Ajax返回一些数据 显示数据没有问题,但我需要它们作为分页。

JQuery的

$(document).ready( function() {

    $(".client_search_option").change(function(){

        var selectedClientTypeVal = "";
        var selectedSmsDecisionVal = "";

        var selectedClientType = $('input[type=radio][name=clientType]:checked');
        var selectedSmsDecision = $('input[type=radio][name=sms_decision]:checked');

        if (selectedClientType.length > 0) {
            selectedClientTypeVal = selectedClientType.val();
        }

        if (selectedSmsDecision.length > 0) {
            selectedSmsDecisionVal = selectedSmsDecision.val();
        }

        //alert(selectedClientTypeVal);
        //alert(selectedSmsDecisionVal);

        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        $.ajax({
            url: 'http://localhost/pages/clientSearchAjax',
            type: 'POST',
            data: {_token: CSRF_TOKEN, selectedClientTypeVal:selectedClientTypeVal,selectedSmsDecisionVal:selectedSmsDecisionVal},
            dataType: 'JSON',
            success: function (data) {
                console.log(data);
            },
            error:function(){
                alert("An error has occured !");
            }         
        });
    });
});

控制器

public function clientSearch(){
    $client_option = Input::get('selectedClientTypeVal');
    $sms_option = Input::get('selectedSmsDecisionVal');

    if($client_option == 'all' && $sms_option == 'all'){
        $ajax_clients = Client::with('clientType')->paginate(5);
    }else{
        $ajax_clients = Client::with('clientType')->where('clienttype_id', $client_option)->where('send_sms', $sms_option)->paginate(5);
    }

    return $ajax_clients->toJson();
}

我如何分享这个Ajax响应,任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的情况,经过一番研究后,我发现了以下链接。可能这可以帮到你。

控制器方法

public function showPosts()
{
    $posts = Post::paginate(5);

    if (Request::ajax()) {
        return Response::json(View::make('posts', array('posts' => $posts))->render());
    }

    return View::make('blog', array('posts' => $posts));
}

jQuery Part

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    $(window).on('hashchange', function() {
        if (window.location.hash) {
            var page = window.location.hash.replace('#', '');
            if (page == Number.NaN || page <= 0) {
                return false;
            } else {
                getPosts(page);
            }
        }
    });

    $(document).ready(function() {
        $(document).on('click', '.pagination a', function(e) {
            getPosts($(this).attr('href').split('page=')[1]);
            e.preventDefault();
        });
    });

    function getPosts(page) {
        $.ajax({
            url: '?page=' + page,
            dataType: 'json',
        }).done(function(data) {
            $('.posts').html(data);
            location.hash = page;
        }).fail(function() {
            alert('Posts could not be loaded.');
        });
    }
</script>

检查this link