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

}
6
  • No, they're not the same. Just compare api.jquery.com/jquery.ajax/#jQuery-ajax-url-settings with developer.mozilla.org/en-US/docs/Web/API/Request/cache Commented Jul 6, 2022 at 14:29
  • Btw, cache: "no-store" is not a header in the fetch API Commented Jul 6, 2022 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 Commented Jul 6, 2022 at 14:33
  • Use cache: "no-cache". It's the closest to jquery's cache: false. Commented Jul 6, 2022 at 14:36
  • @ruleboy21 I will try with ``cache: "no-cache"``` thanks! Commented Jul 6, 2022 at 15:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So what correspondent this cache: false from ajax have in Fetch API ? cache: no cache ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.