如何在 jquery ajax 中启用缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18671648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:11:44  来源:igfitidea点击:

how to enable caching in jquery ajax

javascriptjqueryajaxcaching

提问by Sora

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:trueattribute 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:truereally enable cache and what should i do to make it work ?

当我点击链接时,我有这个功能,内容被设置为 div innerHTML 我cache:true在 jquery ajax 中设置了属性,但是如果我再次点击链接没有缓存显示,ajax 功能仍然会进入服务器端并到达我感到困惑的相同内容cache:true实际上是启用缓存,我应该怎么做才能使其工作?

回答by Khanh TO

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

cache:true是默认值,并不总是从缓存中获取内容。浏览器上项目的缓存能力由以下因素决定:

  • 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.

  • 从源 Web 服务器返回的响应标头。如果标题表明不应缓存内容,则不会缓存。

  • 响应中必须存在诸如 ETag 或 Last-Modified 标头之类的验证器。

From this link

这个链接

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

cache:false有另一个用例总是从服务器加载内容,无论该内容是否被缓存。

The point here is: the cache-ability is determined by the server and cache:trueor cache:falseof the $.ajaxis just to determine whether to look for the cached response or not.

这里的要点是:缓存能力由服务器和确定的cache:truecache:false$.ajax只是确定是否找缓存的响应与否。