Javascript AngularJS:从$http获取缓存结果
我在使用$http调用API时得到缓存结果。以下是AngularJS代码:Javascript AngularJS:从$http获取缓存结果,javascript,angularjs,Javascript,Angularjs,我在使用$http调用API时得到缓存结果。以下是AngularJS代码: $scope.validate = function(){ var encodedUserNameAndPassword = Base64.encode($scope.username + ':' + $scope.password); var decode = Base64.decode(encodedUserNameAndPassword); $http.defaults.headers.p
$scope.validate = function(){
var encodedUserNameAndPassword = Base64.encode($scope.username + ':' + $scope.password);
var decode = Base64.decode(encodedUserNameAndPassword);
$http.defaults.headers.put = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': '*'
};
$http.defaults.headers.common['Authorization'] = 'Basic ' + encodedUserNameAndPassword;
$http({method: 'GET', url: 'http://127.0.0.1:5000/user/jim',cache:false}).
success(function(data, status, headers, config) {
console.log(data);
}).
error(function(data, status, headers, config) {
alert(data);
});
}
任何人都可以指出我遗漏了什么。不要在控制器内部修改$http提供程序。请在配置级别进行修改
var myModule = angular.module('myModule',[]);
myModule.config(['$httpProvider', function($httpProvider) {
//initialize get if not there
if (!$httpProvider.defaults.headers.get) {
$httpProvider.defaults.headers.common = {};
}
$httpProvider.defaults.headers.common["Cache-Control"] = "no-cache";
$httpProvider.defaults.headers.common.Pragma = "no-cache";
$httpProvider.defaults.headers.common["If-Modified-Since"] = "0";
}]);
GET请求大部分时间由IE缓存
然后在$http调用参数中
$http({method: 'GET',
url: 'http://127.0.0.1:5000/user/jim',
headers: {
'Authorization': 'Basic '+ encodedUserNameAndPassword}
}
}).
success(function(data, status, headers, config) {
console.log(data);
}).
error(function(data, status, headers, config) {
alert(data);
});
希望这对您有所帮助。您正在进行服务器端缓存吗?是的,我正在尝试调用本地运行的REST API。我尝试了使用CURL的API,效果很好。你能在chrome private browsing中尝试同样的方法吗?你使用的是哪个AngularJS版本?我使用的是AngularJS 1.2.8。问题是,一旦我获得了成功和数据,即使不返回任何内容,也会返回相同的数据。请尝试$http.defaults.headers.common['Auth-Token']=encodedusernamedpassword;