[英]Angularjs $http request caching on browser back button
Here's my code. 这是我的代码。
$scope.init = function () {
$scope.urlParam = $location.search();
if ($scope.urlParam.token == undefined || $scope.urlParam.id == undefined) {
$scope.is_start = false;
alert("您没有权限投票");
} else {
$http.get('/vote/validate_querystring?token=' + $scope.urlParam.token + "&id=" + $scope.urlParam.id)
.success(function (response) {
if (response.status == 3) {
$scope.is_start = false;
alert("您没有权限投票");
} else if (response.status == 4) {
$scope.is_start = false;
alert("您已完成投票");
} else {
$http.get('/vote/r_vote_setting')
.success(function (response) {
if (response.status == 1) {
$scope.is_start = false;
alert("投票尚未开始");
} else {
$scope.is_start = true;
$scope.voteData = response.data;
}
});
}
})
}
};
I put this function in ng-init
so it will be invoked every time the page is loaded. 我将此函数放在
ng-init
以便每次加载页面时都会调用它。
As you can see, there are two $http.get
in this function. 如您所见,此函数中有两个
$http.get
。 The problem is when I hit the back button to go back to this page, $http.get('/vote/validate_querystring?token=')
would be loaded from browser cache while $http.get('/vote/r_vote_setting')
makes a new request to the server. 问题是,当我按下“ 后退”按钮返回此页面时,将从浏览器缓存中加载
$http.get('/vote/validate_querystring?token=')
,而$http.get('/vote/r_vote_setting')
向服务器发出新请求。 I found this from chrome console. 我是从Chrome控制台找到的。
Request URL:http://localhost:8080/vote/validate_querystring?token=202cb962ac59075b964b07152d234b70&id=1
Request Method:GET
Status Code:200 OK (from disk cache)
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade
Request URL:http://localhost:8080/vote/r_vote_setting
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade
I want to know why this happens and how to make them both send request to the server rather than cache when hitting back button. 我想知道为什么会发生这种情况,以及如何使它们都在单击“后退”按钮时将请求发送到服务器而不是缓存。
You can use cache: false option of $http. 您可以使用$ http的cache:false选项。
$http.get('/vote/validate_querystring?token=' + $scope.urlParam.token + "&id=" + $scope.urlParam.id,cache: false);
Use $httpProvider to set caching false 使用$ httpProvider将缓存设置为false
myModule.config(['$httpProvider', function($httpProvider) {
//initialize get if not there
if (!$httpProvider.defaults.headers.get) {
$httpProvider.defaults.headers.get = {};
}
// Answer edited to include suggestions from comments
// because previous version of code introduced browser-related errors
//disable IE ajax request caching
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
// extra
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}]);
You can also include a time parameter to the call, that would every call unique, thus avoid unwanted caching for only that specific call instead having to mess with $http default settings and potentially affect every other calls on the page 您还可以在呼叫中包含一个时间参数,该参数将使每个呼叫都是唯一的,从而避免仅对该特定呼叫进行不必要的缓存,而不必弄乱$ http默认设置,并可能影响页面上的所有其他呼叫
$http.get({ url: sampleUrl, method: 'GET', params: { time: new Date() } }).error(function (err) { console.log('Error encountered: ' + err); }).success(function (data) { console.log(data); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.