预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Pragma
Posted
技术标签:
【中文标题】预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Pragma【英文标题】:Request header field Pragma is not allowed by Access-Control-Allow-Headers in preflight response 【发布时间】: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 中遇到如下错误:
。
但是当我删除以下代码时,它工作正常。
$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-cache
和 If-Modified-Since
是互斥的。
【参考方案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)
);
【讨论】:
我不得不使用'Pragma': function() return null
,而不是'Pragma': undefined
,如下所述:docs.angularjs.org/api/ng/service/$http#$http-arguments(参数“config”,属性“headers”)以上是关于预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 Pragma的主要内容,如果未能解决你的问题,请参考以下文章
预检响应没有 HTTP ok 状态。 Angular 6 中的 403
OAuth 中的 CORS:对预检请求的响应未通过访问控制检查
预检响应中的 Access-Control-Allow-Headers 不允许授权
预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段内容类型