请求标头字段预检响应中的Access-Control-Allow-Headers不允许使用Pragma

use*_*130 9 ajax cors angularjs

当我试图保留以下代码来禁用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';

}]);
Run Code Online (Sandbox Code Playgroud)

我在chrome中遇到如下错误:

请求标头字段预检响应中的Access-Control-Allow-Headers不允许使用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';
Run Code Online (Sandbox Code Playgroud)

谁能告诉我可能是什么问题?

小智 3

如果您可以在服务器端进行配置以接受这些标头,那么就可以了。否则,您应该删除 $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)
});
Run Code Online (Sandbox Code Playgroud)