我有一个网络服务器,它为特定的 url 提供一个 ETag。当浏览器对该 url 执行请求时,它会提供 http header “If-None-Match”,其中包含服务器先前对该 url 的响应中包含的 ETag 值。现在,如果我以编程方式添加请求 header “If-Modified-Since”并将其设置为 future 或过去的日期(无关紧要),浏览器将停止发送“If-None-Match” header 。我在 FireFox 和 Chrome 中都看到过这种情况(未在任何其他浏览器上测试过)。我无法从 HTTP/1.1 规范中得出结论,情况应该如此。为什么会这样?
这是一个简单的代码示例,可以重现该场景。该代码假定服务器使用 Etag header 进行响应。
var request = new XMLHttpRequest();
request.open("GET", someUrl, true);
request.onreadystatechange = function(){};
// This statement makes the browser stop sending the "If-None-Match" header
request.setRequestHeader("If-Modified-Since", "Sat, 29 Oct 1994 19:43:31 GMT");
request.send(null);
最佳答案
Now, if I programatically add the request-header "If-Modified-Since" and set it to a either a future or past date (doesn't matter), the browser stops sending the "If-None-Match"-header.
如果服务器为给定资源同时提供 Etag 和 Last-Modified header ,则它们不应该:
HTTP/1.1 clients [...] if both an entity tag and a Last-Modified value have been provided by the origin server, SHOULD use both validators in cache-conditional requests. This allows both HTTP/1.0 and HTTP/1.1 caches to respond appropriately.
编辑:当调用 XmlHttpRequest.open()
时,实现使用 nsIHttpChannel
准备请求,其中使用 If-Modified-Since
和 If-None-Match
header (如果它可以找到本地缓存项)。
现在,当您使用 If-(Un)Modified-Since
或 If-None-Match
调用 SetRequestHeader()
时,它'我将从请求中清除两个 header 并设置您的值。
关于http - If-Modified-Since 在浏览器中覆盖 If-None-Match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12834138/