Angularjs的rest get方法在启用了.appcache的情况下无法工作

angularjs rest get method not working with .appcache enabled

本文关键字:情况下 appcache 工作 启用 rest get 方法 Angularjs      更新时间:2023-09-26

我正在使用一个angular应用程序,它正在消耗rest服务,我需要一个.appcache文件来离线工作。

当我把这个头文件放到UI中时:

<html lang=""  ng-app="labsisApp">

在控制器中有这样的代码:

   $http.get('http://localhost:2700/users')
    .success(function(data){
    $scope.users = data;
    })
    .error(function(data, status, headers, config) {
      $scope.onLine = false;
    });

工作!,但当appcache启用时:

<html lang="" manifest="labsis.appcache" ng-app="labsisApp">

Get请求不工作,尽管有这个appache文件:

CACHE MANIFEST
CACHE:
index.html
styles/main.css
scripts/application.js
scripts/angular.min.js
scripts/angular-route.min.js
scripts/lodash.min.js
scripts/restangular.min.js
scripts/main.js

所有重要的文件都在appcachefile中。这个问题只在我启用apache时出现,而angular出现了问题。POST方法工作,但是GET方法不工作

url /users未在您的缓存清单中列出。使用缓存清单文件,客户端将永远不会向服务器请求数据。它总是会在缓存中查找。如果它找不到,它就会失败。

有(至少)两种方法来解决这个问题。一种是在缓存清单中添加NETWORK部分:

NETWORK:
/users

这将允许客户端在线时请求该url,但如果离线请求将失败。使用此解决方案,每当您对NETWORK部分中列出的资源进行请求时,您必须首先检查您是在线还是离线,并且如果您离线,则做一些不同的事情(例如,从localStorage加载数据)。

另一个解决方案是将/users添加到CACHE:部分。