How to tell if an XMLHTTPRequest hit the browser cache(如何判断 XMLHTTPRequest 是否命中浏览器缓存)
问题描述
如果可以判断(在 javascript 执行中)GET XMLHTTPRequest 是否命中浏览器缓存而不是从服务器获取响应?
If it possible to tell (within javascript execution) if a GET XMLHTTPRequest hit the browser cache instead of getting its response from the server?
推荐答案
来自 XMLHttpRequest 规范:
对于用户代理导致的 304 Not Modified 响应生成的条件请求用户代理必须像服务器一样操作用适当的内容给出了 200 OK 响应.
For 304 Not Modified responses that are a result of a user agent generated conditional request the user agent must act as if the server gave a 200 OK response with the appropriate content.
换句话说,浏览器将始终给出状态码 200 OK,即使对于命中浏览器缓存的请求也是如此.
In other words, the browser will always give status code 200 OK, even for requests that hit the browser cache.
但是,规范中还说:
用户代理必须允许作者请求标头覆盖自动缓存验证(例如 If-None-Match 或 If-Modified-Since),在这种情况下必须通过 304 Not Modified 响应.
The user agent must allow author request headers to override automatic cache validation (e.g. If-None-Match or If-Modified-Since), in which case 304 Not Modified responses must be passed through.
因此,有一种解决方法可以使 304 Not Modified 响应对您的 JavaScript 代码可见.
So, there is a workaround to make the 304 Not Modified responses visible to your JavaScript code.
这篇关于如何判断 XMLHTTPRequest 是否命中浏览器缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何判断 XMLHTTPRequest 是否命中浏览器缓存


基础教程推荐
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01