简体   繁体   English

将查询字符串追加到现有的ajax URL cakephp 3

[英]Append query strings to existing ajax url cakephp 3

Let's say that I have btn1 and btn2 which load some data into a div when clicking on them through Ajax like this: 假设我有btn1和btn2,当通过Ajax单击它们时,它们会将一些数据加载到div中,如下所示:

$(document).on('click', '#btn1', function(e){
    var url = "<?= $this->Url->build(['controller' => 'MyController', 'action' => 'my_action', '?' => ['param1' => 'abc']], ['escape' => false]); ?>";

    $('#my_container').load(url);
});

$(document).on('click', '#btn2', function(e){
    var url = "<?= $this->Url->build(['controller' => 'MyController', 'action' => 'my_action', '?' => ['param2' => 'def']], ['escape' => false]); ?>";

    $('#my_container').load(url);
});

As tou can see above, each button call the same controller action but passes different query strings (param1 and param2) with different values. 如上面的tou所示,每个按钮调用相同的控制器操作,但传递具有不同值的不同查询字符串(param1和param2)。

I would like to be able to append the query strings to the current url, instead of just loading the link attahed to each button meaning: 我希望能够将查询字符串附加到当前网址,而不是仅加载附加到每个按钮的链接,这意味着:

If I clicked on btn1, the url that I load will be: 如果单击btn1,则加载的URL将是:

http://example.com/mycontroller/my_action/?param1=abc

But if I then click on btn2, my url should become: 但是,如果我随后单击btn2,则我的网址应为:

http://example.com/mycontroller/my_action/?param1=abc&param2=def

Instead of: 代替:

http://example.com/mycontroller/my_action/?param2=def

So I want to append query strings to the existing url. 所以我想将查询字符串附加到现有的URL。

Keep in mind that those are Ajax calls. 请记住,这些是Ajax调用。 So the currently loaded url is not visible in the browser. 因此,当前加载的URL在浏览器中不可见。

Is there a CakePHP 3 way of doing this? 有CakePHP 3方式吗? I realize that this works already with pagination. 我意识到这已经可以通过分页了。 Anybody knows how to make this work also with custom query strings? 有人知道如何通过自定义查询字符串来实现此功能吗?

Thanks in advance 提前致谢

Are you looking for something like this? 您是否正在寻找这样的东西?

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

This will get you the current querystring items. 这将为您提供当前的查询字符串项。 now you just have to manipulate it and use it. 现在您只需要操纵它并使用它。

Found at https://css-tricks.com/snippets/javascript/get-url-variables/ 可在https://css-tricks.com/snippets/javascript/get-url-variables/找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM