【问题标题】:Why is my AJAX result not ETag-cached (no If-None-Match)?为什么我的 AJAX 结果不是 ETag 缓存(没有 If-None-Match)?
【发布时间】:2018-09-16 10:46:31
【问题描述】:

这是我的 AJAX 函数:

function ajax(url, data) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: "https://xxx",
            data: data,
            method: 'POST',
            timeout: 50000,
            cache: true,
            ifModified: true,
            crossDomain: true,
            success: (data, textStatus, jqXHR) => {
                if (data == '@fail@') reject(data);
                else {resolve(data);}
            },
            error: (jqXHR, textStatus, errorThrown) => {
                reject(errorThrown);
            }
        });
    });
}

正如在 Chrome -> Network(F12) 中观察到的,这是来自服务器的响应标头:

HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Content-Length: 3
ETag: W/"3-R7zlx09Yn0hn29V+nKn4CA"
Date: Fri, 06 Apr 2018 11:39:41 GMT
Connection: keep-alive

请求标头始终相同,即使在后续调用中也是如此:

POST /register HTTP/1.1
Host: xxx:60001
Connection: keep-alive
Content-Length: 0
Accept: */*
Origin: http://localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Referer: http://localhost:8000/index.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

Chrome 不应该在收到 ETag 标头后缓存资源并在后续调用同一 URL 时设置“If-None-Match”标头吗?由于返回的内容相同,我不应该获得 304 而不是 200 的状态码吗?

虽然有时调用其他服务器(例如 Google 地图服务器)中的资源确实会返回 304。

【问题讨论】:

  • 我刚刚意识到,如果我将请求方法从 POST 更改为 GET,ETag 机制将按预期工作。那么 ETag 是否仅适用于 GET 请求?

标签: http express caching http-headers etag


【解决方案1】:

This 确认缓存通常仅限于 GET 请求方法:

但是,常见的 HTTP 缓存通常仅限于缓存对 GET 的响应,并且可能会拒绝其他方法。主缓存键由请求方法和目标 URI 组成(通常只使用 URI,因为只有 GET 请求是缓存目标)

a post in StackOverflow here 也证实了这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2016-06-07
    • 2012-12-28
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多