在jQuery ajax中不使用缓存

发布于 2025-02-13 16:00:01 字数 1138 浏览 0 评论 0原文

我有jQuery ajax的速度,我需要将其转换为获取。我遇到的问题是缓存:false缓存相同:fetch中的标题参数中的“ no-code”? ajax呼叫

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");
    }
  });
}

获取电话:

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);
    });

}

I have this GET in jQuery Ajax and I need to convert it to Fetch. The question I'm having is that cache: false is the same as cache: "no-store" in header param in fetch?
Ajax call

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 call:

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);
    });

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掌心的温暖 2025-02-20 16:00:01

来自 docs

缓存(默认值:true,false for datatype'script'和'jsonp')类型:
布尔

如果设置为false,它将迫使请求的页面不被限制
浏览器。注意:将缓存设置为false仅适用于
头并获取请求。 它通过将“ _ = {timestamp}”附加到
获取参数
。其他类型的参数不需要
请求,除了在IE8中发表帖子的URL的请求
是由get的要求。

From the docs

cache (default: true, false for dataType 'script' and 'jsonp') Type:
Boolean

If set to false, it will force requested pages not to be cached by the
browser. Note: Setting cache to false will only work correctly with
HEAD and GET requests. It works by appending "_={timestamp}" to the
GET parameters
. The parameter is not needed for other types of
requests, except in IE8 when a POST is made to a URL that has already
been requested by a GET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文