2015-09-15 20 views
0

我试图实现对角JS GET请求ETAG,到目前为止,我已经看到了https://github.com/forforf/angular-etag https://www.npmjs.com/package/angular-http-etag 和restangular(一个复杂的但好)。他们都表示它支持GET请求,我认为这意味着我不必在服务器端编写任何代码(使用c#.NET)。ETag的对角JS GET - 我需要检查服务器端

我是否正确地认为,或者我必须使用CacheCow或其他方法在标头中查找ETAG并发送304响应。

只是一个背景,我必须使用ETAG而不是缓存(角),以便从服务器获取最新数据。

+0

如果你发现你的解决方案,请分享 – garrettmac

回答

0

我是angular-http-etag的作者,所以我只能直接说这个模块的功能。它装饰Angular的$http服务,允许您指定要缓存的请求。下面是使用的例子,我在readme.md提供:

angular 
    .module('myApp', [ 
    'http-etag' 
    ]) 
    .config(function (httpEtagProvider) { 
    httpEtagProvider 
     .defineCache('persistentCache', { 
     cacheService: 'localStorage' 
     }) 
    }) 

    .controller('MyCtrl', function ($http) { 
    var self = this 

    $http 
     .get('/my_data.json', { 
     etagCache: 'persistentCache' 
     }) 
     .success(function (data, status, headers, config, itemCache) { 
     // Modify the data from the server 
     data._fullName = data.first_name + ' ' + data.last_name 
     // Update the cache with the modified data 
     itemCache.set(data) 
     // Assign to controller property 
     self.fullName = data._fullName 
     }) 
     // Synchronous method called if request was previously cached 
     .cached(function (data, status, headers, config, itemCache) { 
     self.fullName = data._fullName 
     }) 
     .error(function (data, status) { 
     // 304: 'Not Modified'--Etag matched, cached data is fresh 
     if (status != 304) alert('Request error') 
     }) 
    }) 

的需要的服务器端的唯一的事情是确保服务器发送ETag响应头。你可以在这里找到关于如何在Chrome中检查响应头的信息:https://developers.google.com/web/tools/chrome-devtools/profile/network-performance/resource-loading#view-details-for-a-single-resource

0

那么,对于某些服务器必须支持发布ETags和ASP.NET MVC或Web API不默认。您必须在服务器上使用某种形式的输出缓存。

其中一个项目是菲利普Woj的CacheOutput这里

https://github.com/filipw/Strathweb.CacheOutput