【问题标题】:how to enable caching in jquery ajax如何在 jquery ajax 中启用缓存
【发布时间】:2013-09-11 08:56:53
【问题描述】:
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();

    }

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

【问题讨论】:

    标签: javascript jquery ajax caching


    【解决方案1】:

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

    • 从源 Web 服务器返回的响应标头。如果标题表明 内容不应该被缓存,那么它就不会被缓存。

    • 响应中必须包含 ETag 或 Last-Modified 标头等验证器。

    来自this link

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

    这里的重点是:cache-ability由服务器决定,$.ajaxcache:truecache:false只是决定是否寻找缓存的响应。

    【讨论】:

    • 感谢您的回复,因为我了解您建议我应该手动将我的响应保存在一个数组或其他东西中,而不是检查它是否存在于该数组中,如果它是我从它加载它如果不是我再次调用 ajax 对吗?
    • @Sora:不,如果您需要缓存请求。服务器必须返回带有缓存标头的响应。当浏览器收到响应and there is a cache header, the browser will automatically cache it
    • 如何判断浏览器是否返回缓存头?
    • @Sora:缓存标头是由服务器(而不是浏览器)在响应中设置的。浏览器在收到响应时会检查此标头,以决定是否缓存该响应
    • 我的问题是如何让服务器设置我的缓存头
    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 2011-05-17
    • 1970-01-01
    • 2011-03-02
    • 2018-05-08
    相关资源
    最近更新 更多