2016-03-25 35 views
4

让我得到当我试图让下面的代码来禁用缓存AJAX请求头字段附注不是由访问控制允许报头中的预检响应

angularApp.config(['appConfig', '$httpProvider', function (appConfig, $httpProvider) { 

if (!$httpProvider.defaults.headers.get) { 
    $httpProvider.defaults.headers.get = {}; 
} 

//disable IE ajax request caching 
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; 
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; 
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache'; 

}]); 

我得到这个错误铬错误如下:

请求标头字段在预检响应中,访问控制 - 允许标题不允许使用Pragma。

但是,当我删除下面的代码,其工作正常。

$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; 
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache'; 
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache'; 

任何人都可以告诉我可能是什么问题?

+0

服务器不允许'PRAGMA'标头。我不确定你还需要知道什么。如果你不想使用缓存的响应,那么你应该有一个很好的理由。 – zeroflagL

+0

如何解决此问题 – user1268130

+0

您必须相应地配置服务器。 'no-cache'和'If-Modified-Since'是互斥的btw。 – zeroflagL

回答

1

如果你可以在服务器端配置接受这些头,那就没问题。 否则,您应该删除$ httpProvider.defaulsts中设置的这些标头。 检查下面的代码:

var data = {} 
var httpCoonfig = { 
    headers: {'Pragma': undefined, 'Cache-Control': undefined, 'X-Requested-With': undefined, 'If-Modified-Since': undefined} 
}; 
$http.post('https://www.google.com/', data, httpCoonfig).then(function(response){ 
// console.log(response) 
}, function(response){ 
    console.log(response) 
}); 
相关问题