2016-03-25 56 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) 
}); 
相關問題