9
jQuery("#divProviders img").click(function (e) {
    //alert(jQuery(this)[0].nameProp);
    document.getElementById("TxtProvPic").value = jQuery(this)[0].getAttribute("src"); //jQuery(this)[0].nameProp;


    $.ajax({
        type: "GET",
        url: "Services/TeleCom/EVoucher.aspx",
        data: "ExtFlag=GetProducts&AjaxFalg=SpecialRequest&prov=" + jQuery(this)[0].id.replace("img_", "") + "&pcat=" + document.getElementById("Txhhc").value,
        beforeSend: function () {
            document.getElementById("DivProducts").innerHTML = "";
            document.getElementById("DivLoad").innerHTML = "<img alt='' style='margin-left:300px;margin-top:80px;position:absolute;'  src='App_Themes/VivaTheme/images/bigloading2.gif'/>";
        },
        cache: true,
        success: function (data) {

            var StrResponse;
            StrResponse = data.split('@@@');

            EvoucherFillProductsRes(StrResponse[0]);

        },
        error: function (xhr) {
            alert("responseText: " + xhr.responseText);
        }
    });

    function EvoucherFillProductsRes(res) {
        var slices = res.split("*******");
        document.getElementById("DivProducts").innerHTML = slices[0];
        document.getElementById("DivMenu").innerHTML = slices[1];
        document.getElementById("DivLoad").innerHTML = "";
        jQuery("#BrowsableTwo").scrollable({
            prev: 'a.prodprev',
            next: 'a.prodnext'
        }).navigator();

    }

I have this function when i click to the link a content is set to a div innerHTML i set cache:true attribute in the jquery ajax but if i click again to the link no cache is displayed the ajax function is still going to the server side and reach for the same content i am confused is cache:true really enable cache and what should i do to make it work ?

Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
Sora
  • 2,465
  • 18
  • 73
  • 146

1 Answers1

16

cache:true is the default and does not always get the content from the cache. The cache-ability of an item on the browser is determined by:

  • The response headers returned from the origin web server. If the headers indicate that content should not be cached then it won’t be.

  • A validator such as an ETag or Last-Modified header must be present in the response.

From this link

cache:false has another use case to always load the content from server regardless of whether that content is cached or not.

The point here is: the cache-ability is determined by the server and cache:true or cache:false of the $.ajax is just to determine whether to look for the cached response or not.

Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • 1
    thank you for your reply as i understood you suggest that i should manually save my response in an array or something and than check if it exist or not in that array if it is i load it from it if not i call the ajax again right ? – Sora Sep 07 '13 at 09:10
  • @Sora: no, if you need a request to be cached. The server has to return a response with a cache header. When the browser receives the response `and there is a cache header, the browser will automatically cache it` – Khanh TO Sep 07 '13 at 09:13
  • 1
    how can i tell if the browser is returning a cache header ? – Sora Sep 07 '13 at 09:15
  • @Sora: the cache header is set on the response by the server (not by the browser). The browser checks for this header when receiving the response to decide whether to cache that response – Khanh TO Sep 07 '13 at 09:18
  • 1
    than my question would be how to let the server set my cache header – Sora Sep 07 '13 at 09:27
  • @Sora: you could take a look at http://stackoverflow.com/questions/945898/cache-control-headers-in-asp-net and http://stackoverflow.com/questions/9234044/asp-net-mvc-and-ie-caching-manipulating-response-headers-ineffective. Notice the `Cache-Control` header in the response – Khanh TO Sep 07 '13 at 09:27
  • When would someone want to do cache:false? I don’t seem to see a topic –  Jan 23 '19 at 20:36
  • @Jeff Bezos: you would use `cache:false` if you want to always load latest content from server – Khanh TO Jan 26 '19 at 05:38