1

I'm new to angular and having trouble with a service that always worked, until it didn't. My service has the following call.

this.getForms = function() {
    return $http.get("/forms").
    then(function(response) {
        return response;
    }, function(response) {
        alert("Error finding forms.");
    });
};

When the page is refreshed (safari) getForms is trigged, $http.get is called, my Node.js/Express server forms endpoint returns the forms data properly.

app.get("/forms", function (req, res) {
    Form.find({}, function (err, docs) {
        if (err) {
            server.handleError(res, err.message, "Failed to get forms.");
        } else {
            res.status(200).json(docs);
        }
    });
});

But instead of the JSON I get a 304 error, which indicates the data is available in cache. But the 304 response header has an empty data string, so it's not returning any data from the cache.

My questions are

1) Why is it calling my server if the data is available in cache?

2) How can I tell it to not cache this call so the page can update with forms correctly?

SafeFastExpressive
  • 3,637
  • 2
  • 32
  • 39

1 Answers1

5

Edit: Looks like there could be a potential issue with Safari. See this post as well. NodeJS/express: Cache and 304 status code

You might be misunderstanding what the 304 status code means. 304 is not an error; it just means that the resource you are requesting from the server has not changed. The response is meant to not contain data as it expects the client to have cached it somewhere.

For example, web browsers will try to cache an image so the server doesn't have to send it over the network again if the user reloads the page or returns to it later. However, the browser needs a way to know whether the image is up to date or not. If the server sends a 304 the browser knows it can continue to use the copy in its cache.

In your case you should implement some sort of caching to return the previous response if you recieve a 304. Alternatively, I believe you could add these headers to the request to force it to return data

 Cache-Control: no-cache, no-store, must-revalidate
 Pragma: no-cache
 Expires: 0
Community
  • 1
  • 1