如何修复浏览器缓存和 JSON 的 notmodified
响应? jQuery.ajax({ifModified:true,cache:true})
JSON 请求在 data
响应时中断。
首次浏览器请求 http://localhost/api返回状态 200 OK
,然后返回 304 Not Modified
$.ajax({
type:"GET",
url:'http://localhost/api', // {"content"="Hello!"}
dataType:'json',
cache:true,
ifModified:true, // Lets respond `304:notmodified`
success:function(data,textStatus,jqXHR){
console.debug(jqXHR.status+':'+textStatus);
console.debug(data); // Why on repeated request returns `undefined`?
}
});
XHR 第一次返回正常:
200:success
Object {content="Hello!"}
但下次返回数据
未定义:
304:notmodified
undefined
如何解决?看来是 jQuery 1.5.1 的 bug。 预期结果:
304:notmodified
Object {content="Hello!"}
最佳答案
我相信这就是它应该如何工作的304不返回任何数据它只是告诉你它没有改变。
但是,如果您尚未在内存中获取数据,那么我确实看到了问题,那么您需要某种方法从浏览器缓存中获取数据。因此我认为解决方案是编写代码来缓存数据。
我不确定 HTTPS 如何与 etag 一起使用,但是,HTTPs 数据并不总是被缓存(浏览器和版本之间的方法和行为不同),因此如果 etag 起作用,您可能仍然需要实现自己的安全缓存。
关于jquery - 如何修复浏览器缓存和未修改的 JSON 响应? jQuery.ajax({ifModified :true, cache:true}) JSON 请求在数据响应时中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5118017/