简体   繁体   English

jQuery Ajax GET请求将不会缓存

[英]jQuery Ajax GET request won't cache

I am trying to take an app of mine offline. 我正在尝试使我的应用程序离线。 I make a few GET requests to load modules into the app. 我提出了一些GET请求,以将模块加载到应用程序中。 I have added these files to my cache manifest so the request would still work. 我已将这些文件添加到我的缓存清单中,因此该请求仍然有效。

function loadReportDiv(ajaxUrl) {
    $.ajax({
        type : "GET",
        cache: true,
        url : ajaxUrl,
        success : function(data) {
            console.log("REPORTS: " + ajaxUrl + "... load complete");
            $("#reports_menu_wrapper").after(data);
        },
        error : function(jqXHR, textStatus, errorThrown) {
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });
}
loadReportDiv("sales_reports.html");
loadReportDiv("call_reports.html");

I am running the app in chrome and setting the network to "offline" in my device emulator (emulating an iPad). 我正在Chrome中运行该应用程序,并在我的设备模拟器(模拟iPad)中将网络设置为“离线”。 The files I am loading with ajax have their own javascript files which are referenced in the .html files. 我使用ajax加载的文件具有自己的javascript文件,这些文件在.html文件中引用。 When I try loading sales_reports.html and call_reports.html chrome fails to load the files and spits out the url it was trying to GET, which is: js/call_reports.js?_=1415997141636 . 当我尝试加载sales_reports.htmlcall_reports.html chrome无法加载文件并吐出尝试获取的url,即js/call_reports.js?_=1415997141636

The appended ?_=[timestamp] to the filepath, is breaking my app and I can't seem to prevent it from happening. 附加到文件路径的?_=[timestamp]破坏了我的应用程序,我似乎无法阻止它的发生。 As you can see I set my cache mode for my ajax request to true in an attempt to prevent this behaviour. 如您所见,我将我的ajax请求的缓存模式设置为true,以试图防止这种行为。

EDIT: 编辑:

The version of jQuery I'm using is 1.11.1. 我正在使用的jQuery版本是1.11.1。

I have tried coding the ajax request in vanilla javascript and am having the same problem. 我已经尝试在普通javascript中编码ajax请求,并且遇到了同样的问题。

function loadReportDiv(ajaxUrl) {
  var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        console.log("REPORTS: " + ajaxUrl + "... load complete");
        $("#reports_menu_wrapper").after(xmlhttp.responseText);
    }
  }
  xmlhttp.open("GET", ajaxUrl);
  xmlhttp.send();       
}

The error is thrown in my jQuery file at line 9631: // Do send the request // This may raise an exception which is actually // handled in jQuery.ajax (so no try/catch here) xhr.send( ( options.hasContent && options.data ) || null ); 该错误在我的jQuery文件的第9631行引发://发送请求//这可能会引发一个异常,该异常实际上是在jQuery.ajax中处理的(因此这里没有try / catch)xhr.send((options。 hasContent && options.data)|| null);

But it is also still appending the time stamp to the request url. 但是,它仍然会将时间戳添加到请求网址。

Once again, any help would be greatly appreciated! 再一次,任何帮助将不胜感激! Thanks. 谢谢。

I finally figured it out! 我终于想通了!

I have a bunch of .js libraries in my app and I guess somewhere along the way one of them is forcing ajax requests to not be cached. 我的应用程序中有一堆.js库,我猜它们中的某个地方强迫ajax请求不被缓存。 I have added this to my index.html file to counter-act that. 我已将此添加到我的index.html文件中以抵消它。

    <script>
        $(function(){
            $.ajaxSetup({
                cache: true
            });
        });
    </script>

Everything is working a-ok now! 现在一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM