简体   繁体   English

如何获得先前的ajax请求

[英]How to get to previous ajax request

I'm working on one script which contains a lot of ajax requests. 我正在处理一个包含很多ajax请求的脚本。 Script is working absolutely fine, I have there a lot of dynamic content which is changing depends on user's select or search input. 脚本工作正常,我有很多动态内容,这些内容根据用户的选择或搜索输入而变化。

I've started looking for how I can to manipulate the browser history and basically make these request saved in browser history. 我已经开始寻找如何操纵浏览器历史记录并将这些请求保存在浏览器历史记录中的方法。 I've went through MDN - Manipulating the browser history . 我经历了MDN - Manipulating the browser history I've got the general idea of this article, but I'm a bit struggling with implementing it in my project. 我已经了解了本文的总体思路,但是在我的项目中实现它有点困难。

Here is a bit of code for live search part: 这是实时搜索部分的代码:

$('#search').keyup(Foundation.utils.throttle(function(e) {
    var searchField = $('#search').val();
    var regex = /[\D]+/;

    $('.loader').show();

    if (regex.exec(searchField)) {
      $.get("getEventsWithVideos.php?text=" + searchField, function(data) {
    });

    $.get("getClubsWithVideos.php?text=" + searchField, function(data) {
    });

    } else {

      // If searchField is a number
      $.get("getMembersWithVideos.php?text=" + searchField, function(data) {
        $events.empty();
        $clubs.empty();
        $members.empty();
        $membersdeep.empty();
        $members.append("<h4>Members</h4>");
        var vals = jQuery.parseJSON(data);

        $.get("getVideosDyn.php?membershipno=" + vals['Member']['membership_no'], function(data) {
        }); 

  });

}

}, 400));

Can please someone point my up with snippet based on my code, how I can create a link for each request to be saved in browser history so I could create a button and add on.click goto 1 request back. 可以请某人根据我的代码向我介绍代码段,如何为要保存在浏览器历史记录中的每个请求创建链接,以便创建按钮并添加。单击goto 1 request back。

Thank you in advance 先感谢您

I am proposing a solution using internal page links. 我正在提出使用内部页面链接的解决方案。 When the user goes back or forward in the browser or you trigger the back() event you will have to bind to the "onpopstate" event and use the state object to get the window into the correct state. 当用户在浏览器中前进或后退或触发back()事件时,将必须绑定到“ onpopstate”事件,并使用状态对象将窗口置于正确的状态。

$('#search').keyup(Foundation.utils.throttle(function(e) {
    var searchField = $('#search').val();
    var regex = /[\D]+/;

    $('.loader').show();

    if (regex.exec(searchField)) {
      $.get("getEventsWithVideos.php?text=" + searchField, function(data) {
         //I assume more code goes here and you also want set this as a history entry
         //add any parameters you need here to build this state when someone navigates to this                page
         var stateObj = { type: "getEventsWithVideos", parameter: searchField };

        //the third parameter is what will display in the browser, when you are on this state
         window.history.pushState(stateObj, "get events with videos", "#eventsWithVideos" + searchField);      
    });

    $.get("getClubsWithVideos.php?text=" + searchField, function(data) {
         var stateObj = { type: "getClubsWithVideos", parameter: searchField };             
         window.history.pushState(stateObj, "get clubs with videos", "#clubsWithVideos" + searchField);  
    });

    } else {

      // If searchField is a number
      $.get("getMembersWithVideos.php?text=" + searchField, function(data) {
        $events.empty();
        $clubs.empty();
        $members.empty();
        $membersdeep.empty();
        $members.append("<h4>Members</h4>");
        var vals = jQuery.parseJSON(data);

         var stateObj = { type: "getMembersWithVideos", parameter: searchField };             
         window.history.pushState(stateObj, "get clubs with videos", "#membersWithVideos" + searchField)

        $.get("getVideosDyn.php?membershipno=" + vals['Member']['membership_no'], function(data) {

        }); 

  });

}

}, 400));

You will also need to handle the onpopstate event to redo the ajax calls and get your page in the correct state when the user goes back or forward 您还需要处理onpopstate事件以重做ajax调用,并在用户后退或前进时将页面置于正确的状态

window.onpopstate = function(e){
   if (e.state && e.state.type) {
      switch (e.state.type)
       {
         case "getEventsWithVideos": {//do ajax call for the first one here, to get the page state correct
               $.get("getEventsWithVideos.php?text=" + searchField, function(data) {});
           break;
          }
         case "getClubsWithVideo": //do ajax call for the second one here etc
         case "getMembersWithVideos":
       }
    }

  };

Here is a simple plunker illustrating how calling pushstate changes the url of the page. 这是一个简单的例子,说明了如何调用pushstate更改页面的url。 I am not using Ajax get queries in the plunker, but the state is changed by the text the user types in the search box. 我不是在插件中使用Ajax获取查询,而是通过用户在搜索框中键入的文本来更改状态。

http://plnkr.co/edit/HJTrW9GyKFduarl75IAd?p=preview http://plnkr.co/edit/HJTrW9GyKFduarl75IAd?p=preview

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

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