【问题标题】:Add cache to ajax json request将缓存添加到 ajax json 请求
【发布时间】:2013-12-12 22:28:16
【问题描述】:

我有一个基本的 ajax 调用来解析一个 json 文件。我需要确保每次有人访问该页面时我都不会点击提要。我将如何添加某种缓存,以便每隔 2 小时才请求一次提要?

 $(function () {
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "my.json",
                success: function (data) {
                // do something with the data
        }
    });

【问题讨论】:

    标签: javascript jquery ajax json caching


    【解决方案1】:

    也许您可以使用 cookie 存储您的时间并检查每次以了解 2 小时的时间间隔,然后您可以调用您的函数获取最新的提要。

    【讨论】:

      【解决方案2】:

      默认情况下,应该被缓存。您可以如下所示显式设置选项。

      $(function () {
              $.ajax({
                  type: "GET",
                  cache: true,
                  dataType: "json",
                  url: "my.json",
                  success: function (data) {
                  // do something with the data
          }
      });
      

      您还可以对页面上的所有 ajax 调用使用以下语句。

      $.ajaxSetup({cache: true});
      

      【讨论】:

        猜你喜欢
        • 2020-05-23
        • 1970-01-01
        • 2010-10-13
        • 1970-01-01
        • 1970-01-01
        • 2014-11-25
        • 1970-01-01
        相关资源
        最近更新 更多