问题
we are using the Ionic framework and AngularJS to create a mobile app with phone gap build. We have a call to our api to get an array of items using $http.get, we are adding an Etag in the header if we already have data in cache. If the data on the server has not changed we get a 304 error which is ok. We are having trouble modifying the data property on the response object form the server to our chached data in local storage.
Any help is appreciated. Thanks
return $http({
method: 'GET',
url: 'http:example.com',
params: params,
headers: customHeader
})
.success(function(data, status, headers, config) {
// do some stuff
})
.error(function(data, status, headers, config) {
if(status == '304') {
alert('this data has not changed');
data = chachedData
// change data to chached data??
}
});
回答1:
HTTP 304 header code means Not modified, it's not an error. According to the RFC 2616 of HTTP 1.1, the server only sends back headers, not the response which tells the browser to use the response that it had already in cache.
It should be used to avoid heavy network traffic.
But in your case, if you want to do some cache invalidation, this is not the way (but I don't think this is what you wan't to do)
On the other hand, angular will always put 200 in status (even if it is a 304) and you shouldn't have to bother about keeping your data up to date, since you retrieve the fresher value each time (without bothering if it's from a cache in the server or fresh data from the server).
来源:https://stackoverflow.com/questions/31598601/angularjs-http-304-error-modify-data-property