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.

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条