0

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

}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Christian
  • 53
  • 1
  • 8
  • No, they're not the same. Just compare https://api.jquery.com/jquery.ajax/#jQuery-ajax-url-settings with https://developer.mozilla.org/en-US/docs/Web/API/Request/cache – Bergi Jul 06 '22 at 14:29
  • Btw, `cache: "no-store"` is **not** a header in the `fetch` API – Bergi Jul 06 '22 at 14:32
  • @Bergi I understand, so what would be the correspondent in fetch API ? As I posted, I need to convert that Ajax to Fetch – Christian Jul 06 '22 at 14:33
  • Use `cache: "no-cache"`. It's the closest to jquery's `cache: false`. – ruleboy21 Jul 06 '22 at 14:36
  • @ruleboy21 I will try with ``cache: "no-cache"``` thanks! – Christian Jul 06 '22 at 15:05
  • @Besworks There are several opinions.. I think I will try with ```cache: "no-cache"``` instead of ```cache: "no-store"```. Thanks – Christian Jul 06 '22 at 15:06

1 Answers1

0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
IT goldman
  • 14,885
  • 2
  • 14
  • 28