weixin_33716941 2016-02-04 15:45 采纳率: 0%
浏览 3

Ajax首次通话

I have a script that process data with some ajax call, depending on the user clicks and changes the call is done once again(works fine).

$.ajax({
   type: "POST",
   url: "process_data.php",
   data: { action: "first_action",data:data},
   dataType: 'json',
   success:function(result){
       //process
   },
   error: function(XMLHttpRequest, textStatus, errorThrown) {
   }
 });

To save time I want to execute some queries in my process_data.php file only during the first ajax call and save them into variables so I dont have to execute the same queries in every call. How to catch ajax first call ?

  • 写回答

1条回答 默认 最新

  • 7*4 2016-02-04 15:48
    关注

    I believe you can set a $_SESSION flag for checking if the client's request has this call executed.

    $_SESSION["exed"] = false;
    
    if (!$_SESSION["exed"]) {
      // Execute The Stuff
      // Reset the session
      $_SESSION["exed"] = true;
    }
    

    Or even if the above looks stupid, at first the session will not be set.

    if (!isset($_SESSION["exed"])) {
      // Execute The Stuff
      // Reset the session
      $_SESSION["exed"] = true;
    }
    

    Or if you want to do it only once for all the clients, then you can use a file on the server and manage it. Let me know if this would work.

    Also, care must be taken to ensure the session has been started. Please refer to the PHP Manual page on session_start.

    评论

报告相同问题?