XMLHttpRequests 是否缓存响应标头?
如果我发出 XMLHttpRequest 并且浏览器缓存响应,它是否也会缓存 HTTP 响应标头?也就是说,下次我发出相同的请求时,我会从 response.getResponseHeader
返回相同的值吗?
这与浏览器相关吗?
If I make an XMLHttpRequest and the browser caches the response, will it also cache the HTTP response headers? That is, the next time I make the same request, will I get the same values back from response.getResponseHeader
?
Is this browser-dependent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确信至少主要浏览器不会尝试缓存标头。但是,如果您想完全阻止缓存,则可能需要发送特殊标头。如果您想快速测试缓存行为,这里有一个页面:
http:// www.mnot.net/javascript/xmlhttprequest/cache.html
如果您想了解实际情况,我建议您使用 Wireshark 之类的数据包嗅探器并亲自查看。我可以想象浏览器至少会对 XmlHttpRequest 执行 HEAD 请求,即使它为您提供了缓存的正文,但我可能是错的。
I'm certain at least the major browsers don't try to cache the headers. However, if you want to prevent caching altogether, you may need to send special headers. If you want to do a quick test of caching behaviors, there's a page here:
http://www.mnot.net/javascript/xmlhttprequest/cache.html
And I recommend if you want to see what's actually happening, that you go get a packet sniffer such as Wireshark and see for yourself. I can imagine the browser at least performs a HEAD request for an XmlHttpRequest even if it gives you the cached body, but I could be wrong.
标头要么不被缓存,要么不被重用。由于发送了请求,因此会收到标头,然后才能确定该请求在缓存中是否有效。新的标头已经被下载,因此没有必要重用旧的标头。
仅缓存响应正文。
您当然可以很容易地测试这一点。向静态资源(img 或 txt 文件或其他文件)发出 XHR 请求并检查
Date
标头。我不认为它依赖于浏览器。浏览器缓存和重用 HTTP 标头会非常非常奇怪。
编辑
jQuery 添加(我认为默认情况下)反缓存参数到 GET 请求(非常烦人),这可以回答你的问题:没有任何东西像那样被缓存。
Headers are either not cached or not reused. Since a request is sent, headers are received and only then can be decided if this request is valid in the cache. The new headers have then already been downloaded, so no point in reusing the old ones.
Only the response body is cached.
You could ofcourse very easily test this. Make a XHR request to a static resource (img or txt file or something) and check the
Date
header.I don't think it's browser dependant. A browser caching and reusing HTTP headers would be very, very strange.
edit
jQuery adds (by default I think) anti caching arguments to GET requests (very annoying), which would sort-of answer your question: nothing is cached like that.