简体   繁体   English

缓存ajax调用的结果以防止多次运行

[英]Cache the results from the ajax calls to prevent running them multiple times

In my project i have a php file that launch a js function that make an ajax call. 在我的项目中,我有一个php文件,该文件启动js函数以进行ajax调用。 My problem is, if a user refresh php page on browser function re-make ajax call and so on. 我的问题是,如果用户刷新浏览器功能上的php页面,请重新进行ajax调用,等等。 I need to cache the first result of ajax execution so that if the refresh is done being used data in the cache and not the ajax call is made again. 我需要缓存ajax执行的第一个结果,以便在刷新完成后使用缓存中的数据,而不是再次进行ajax调用。

My call is 我的电话是

if (xmlHttp)
    {
    // try to connect to the server
    try
    {
      xmlHttp.open("GET", "top10.php?P1="+oStxt, true);
      xmlHttp.onreadystatechange = handleRequestStateChange10;
      xmlHttp.send(null);
    }
        // display the error in case of failure
        catch (e)
    {
        alert("Can't connect to server:\n" + e.toString());
    }

How can i prevent multiple ajax call from browser page refresh? 如何防止浏览器页面刷新多个Ajax调用?

Thanks in advance 提前致谢

I would suggest the following flow: 我建议以下流程:

  • Before making the request hash your request URL (MD5?) 在对请求进行哈希处理之前,您的请求URL(MD5?)
  • Check to see if your localStorage (or similar) has a value for this hash 检查您的localStorage (或类似存储库)是否具有此哈希值
  • If no value 如果没有价值
    • do your request 做你的要求
    • save returned data in localStorage with hash as key 将返回的数据保存在localStorage中,哈希为键
    • render page 呈现页面
  • If value 如果值
    • load from localStorage 从localStorage加载
    • render page 呈现页面

Easy! 简单!

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

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