【问题标题】:Using no cache in jQuery ajax在 jQuery ajax 中不使用缓存
【发布时间】:2022-07-06 22:30:14
【问题描述】:

我在 jQuery Ajax 中有这个 GET,我需要将它转换为 Fetch。我遇到的问题是 cache: false 与 fetch 中的标头参数中的 cache: "no-store" 相同吗? 阿贾克斯调用

function ajaxLogoutDetailsApi() {
  $.ajax({
    type: "GET",
    url: "OIDCGetLogoutDetails",
    async: false,
    cache: false,
    data: "json",
    success: function (data, status, xhr) {
      data = data.replace('\/\*', '');
      data = data.replace('\*\/', '');
      var dataJson = JSON.parse(data);
      if (dataJson.logoutUrl != null) {
        document.location.href = dataJson.logoutUrl;
      }
    },
    error: function (xhr, status, err) {
      console.log("error in ajaxLogoutDetailsApi");
    }
  });
}

Fetch 调用:

function ajaxLogoutDetailsApi() {
  
  const endpoint = 'OIDCGetLogoutDetails';
  
  fetch(endpoint, {cache: "no-store"})
    .then(json => {
      const updated = json
        .replace('\/\*', '')
        .replace('\*\/', '');
      const data = JSON.parse(updated);
      if (data.logoutUrl) {
        window.location.href = data.logoutUrl;
      }
    })
    .catch(error => {
      console.error('Error:', error);
    });

}

【问题讨论】:

标签: javascript jquery ajax fetch


【解决方案1】:

来自docs

cache(默认:true,对于 dataType 'script' 和 'jsonp' 为 false) 类型: 布尔值

如果设置为 false,它将强制请求的页面不被缓存 浏览器。注意:将缓存设置为 false 仅适用于 HEAD 和 GET 请求。 通过将“_={timestamp}”附加到 获取参数。其他类型不需要该参数 请求,但在 IE8 中,当对已经存在的 URL 进行 POST 时 已被 GET 请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-20
    • 2013-09-11
    • 2012-10-04
    • 2018-02-10
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    相关资源
    最近更新 更多