如何使用Ajax保持请求值搜索?

问题描述 投票:1回答:1

我有laravel应用程序,并使用laravel创建简单的搜索,此搜索paginate Ajax一切都很好,但是当搜索某些内容并单击分页的第二页时,它会显示所有users

我进行搜索,发现将此代码添加到我的代码中:

  {{$users->appends(request()->query())->links()}}

但是这里我将Ajax与分页一起使用!所以我该如何解决?

链接:

http://code.test/search?q=test#1

。#1表示首页,等等。

Ajax代码:

<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;
        $('html, body').animate({ scrollTop: 0 });
    });
}
</script>
ajax laravel
1个回答
0
投票
答案是:仅将Ajax代码中的url更改为此:

url:"search?q="+ "{{ Input::get('q') }}" +"&page="+page,

© www.soinside.com 2019 - 2024. All rights reserved.