【问题标题】:Request header field Pragma is not allowed by Access-Control-Allow-Headers in preflight response预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Pragma
【发布时间】:2016-07-13 03:37:33
【问题描述】:

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

}]);

我在 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';

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

【问题讨论】:

  • 服务器不允许PRAGMA 标头。我不确定你还需要知道什么。如果您不想使用缓存响应,那么您应该有一个很好的理由。
  • 我该如何解决这个问题
  • 您必须相应地配置服务器。 no-cacheIf-Modified-Since 是互斥的。

标签: angularjs ajax cors


【解决方案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)
});

【讨论】:

猜你喜欢
  • 2016-05-15
  • 2016-04-24
  • 2019-02-21
  • 2017-12-20
  • 2017-01-30
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多