dongyong2906 2018-01-20 09:31
浏览 51
已采纳

在url中的ajax请求之后捕获和更新变量值

I would really appreciate some help on this. I have a page that shows products in a store using laravel pagination. I have filters on the page based on brands, category, and available products. for filtering the products I am using a checkbox. if a checkbox is checked I use ajax get request and send status via URL to a controller to filter available products.

status = 1 is for available products, and status = 0 is for all products.Url is looks like this:

/Collections/Newest_Items?status=1&page=2

Here is the situation. I want to know if is it possible to change the variable value in URL and regenerate the URL base on the page number and new filters dynamically? Is it a way to get the URL of the page using jquery and change the values and then change the Url with window.history.pushState("", "", URL);? Here is my ajax:

       $(document).on('click', "#only_available", function () {

                if ($('#only_available').is(':checked')) {
                    var status = 1;
                    url = '/Collections/Newest_Items?status='+status;
                } else {
                    var status = 0;
                    url = '/Collections/Newest_Items';
                }

                window.history.pushState("", "", url);

                $.ajax({
                    url: '/Collections/Newest_Items',
                    type: "GET",
                    data: {status: status},
                    cash: false,
                    success:
                        function (response) {
                            $('#products-load').html(response);
                        }

                });
            });
        });

I do this by writing the URL by myself. In this situation, I must write the URL after every filter applied to the page. this way I cant get the page the user currently in and it goes back to the first page. But what I want to achieve here is, I want to make the Url dynamically with page number the user currently on with all filters applied to it.

  • 写回答

3条回答 默认 最新

  • douzhang8840 2018-01-20 12:53
    关注

    tnx to @CesarBielich and @Sokies I finally manage to solve the problem. they give me part of the answer but not all.I made it unique to my question:

    what we need here is the path and the parameters that nested in URL. so for getting the path of the route, we must use window.location.pathname and for getting all the parameters must use window.location.search. after that, we must combine the path and params so that the URL comes out of it. then we must add the new parameter like status after that. So that all the parameters can be accessed by the controller. both the old params and the new one. this way laravel pagination knows what url to make, in the href links to other pages.

            $(document).on('click', "#only_available", function () {
    
                    if ($('#only_available').is(':checked')) {
                        var status = 1;
                    } else {
                        var status = 0;
                    }
    
                    var params = window.location.search;
                    var path = window.location.pathname;
    
                    var old_url = path+params;
                   var url = old_url+'&status=' + status;
    
                    window.history.pushState("", "", url);
    
                    $.ajax({
                        url: url,
                        type: "GET",
                        cash: false,
                        success:
                            function (response) {
                                $('#products-load').html(response);
                            }
    
                    });
                });
            });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开