[英]jQuery Ajax GET request won't cache
我正在尝试使我的应用程序离线。 我提出了一些GET
请求,以将模块加载到应用程序中。 我已将这些文件添加到我的缓存清单中,因此该请求仍然有效。
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");
我正在Chrome中运行该应用程序,并在我的设备模拟器(模拟iPad)中将网络设置为“离线”。 我使用ajax加载的文件具有自己的javascript文件,这些文件在.html文件中引用。 当我尝试加载sales_reports.html
和call_reports.html
chrome无法加载文件并吐出尝试获取的url,即js/call_reports.js?_=1415997141636
。
附加到文件路径的?_=[timestamp]
破坏了我的应用程序,我似乎无法阻止它的发生。 如您所见,我将我的ajax请求的缓存模式设置为true,以试图防止这种行为。
我正在使用的jQuery版本是1.11.1。
我已经尝试在普通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();
}
该错误在我的jQuery文件的第9631行引发://发送请求//这可能会引发一个异常,该异常实际上是在jQuery.ajax中处理的(因此这里没有try / catch)xhr.send((options。 hasContent && options.data)|| null);
但是,它仍然会将时间戳添加到请求网址。
再一次,任何帮助将不胜感激! 谢谢。
我终于想通了!
我的应用程序中有一堆.js库,我猜它们中的某个地方强迫ajax请求不被缓存。 我已将此添加到我的index.html
文件中以抵消它。
<script>
$(function(){
$.ajaxSetup({
cache: true
});
});
</script>
现在一切正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.