Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 网页中的AngularJS缓存问题_Html_Angularjs - Fatal编程技术网

Html 网页中的AngularJS缓存问题

Html 网页中的AngularJS缓存问题,html,angularjs,Html,Angularjs,我有一个Spring AngularJS web应用程序,其中我使用Spring控制器获取JSON数据,并在angular factory中使用$http方法使用这些数据,并为此编写必要的angular控制器。我使用html文件来显示这些数据。我在html页面中动态获取表单字段名和字段类型。我的问题是,当我最初登录并转到相应的页面时,我得到的似乎是缓存问题。参数显示为html页面中的参数。只有在刷新页面后,我才能获得正确的数据。有人能帮我弄清楚发生了什么吗?您可以在JSON URL中添加时间戳

我有一个Spring AngularJS web应用程序,其中我使用Spring控制器获取JSON数据,并在angular factory中使用$http方法使用这些数据,并为此编写必要的angular控制器。我使用html文件来显示这些数据。我在html页面中动态获取表单字段名和字段类型。我的问题是,当我最初登录并转到相应的页面时,我得到的似乎是缓存问题。参数显示为html页面中的参数。只有在刷新页面后,我才能获得正确的数据。有人能帮我弄清楚发生了什么吗?

您可以在JSON URL中添加时间戳

var JSONURL = "your json url here";
var t = getTime();

$http({
  method: 'GET',
  url: JSONURL + "?t=" + t
  }).success(function () {
});

虽然Lee的答案是正确的,而且jQuery在很多年前是如何使用AJAX请求实现的(我不知道现在是否仍然如此)。但它是黑的

正确的方法是在服务器响应上正确设置
缓存控件
Pragma
过期
头。既然您在服务器上使用的是Spring,那么我假设您使用的是某种JSPServlet容器(比如ApacheTomcat)对吗

如果是这样的话,Tomcat具有内置功能,您可以通过
web.xml
文件执行此操作。看看
org.apache.catalina.filters.ExpiresFilter
类。以下是一个小样本:

  <filter>
    <filter-name>ExpiresFilter</filter-name>
    <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
    <init-param>
      <param-name>ExpiresExcludedResponseStatusCodes</param-name>
      <param-value>302, 304, 404, 500, 503</param-value>
    </init-param>
    <init-param>
      <param-name>ExpiresByType text/html</param-name>
      <param-value>access plus 1 day</param-value>
    </init-param>
    <init-param>
      <param-name>ExpiresByType application/javascript</param-name>
      <param-value>access plus 1 day</param-value>
    </init-param>
    <init-param>
      <param-name>ExpiresByType image</param-name>
      <param-value>access plus 1 month</param-value>
    </init-param>
  </filter>

过期过滤器
org.apache.catalina.filters.ExpiresFilter
ExpiresExcludedResponseStatusCodes
302, 304, 404, 500, 503
ExpiresByType文本/html
访问加1天
ExpiresByType应用程序/javascript
访问加1天
ExpiresByType映像
访问加1个月
由于省略了
application/json
,因此不会缓存任何json请求或响应。此设置在我们的web应用程序中运行良好

我要注意的一件事是,设置了这些标题后,简单地刷新浏览器页面将不起作用——它仍然会从本地缓存中获取。这就是说,Chrome可以在调试控制台打开时禁用缓存,因此这个问题很容易缓解