0
votes

Here is my AJAX function:

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);
            }
        });
    });
}

As observed in Chrome -> Network(F12), this is the response header from the server:

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

The request header is always identical, even in subsequent calls:

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

Shouldn't Chrome, upon receiving an ETag header, cache the resource and set the 'If-None-Match' header on subsequent calls to the same URL? Shouldn't I obtain a status code of 304 instead of 200 as the returned content is the same?

The calls to the resources in other servers such as the Google Map server do return 304 sometimes though.

1
I just realized that if I changed the request method from POST to GET, the ETag mechanism would work as expected. So does ETag only work with GET requests?Chong Lip Phang

1 Answers

0
votes

This confirms that caching is generally limited to GET request methods only:

However, common HTTP caches are typically limited to caching responses to GET and may decline other methods. The primary cache key consists of the request method and target URI (oftentimes only the URI is used as only GET requests are caching targets)

This is also confirmed in a post in StackOverflow here.