Api错误:请求标头字段在预检响应中,Access-Control-Allow-Headers不允许使用Cache-Control

时间:2018-04-09 07:42:18

标签: php ajax api

我正在尝试从API获取数据。我用邮递员测试了它,它给了我完美的回应。当我从邮递员复制代码并将其粘贴到我的测试文件中时,我收到此错误

  

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

以下是我正在使用的代码。

    <? php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Cache-Control,x-secret-key');
?>
<html>
<head>
<title>TestApi</title>
</head>
<body>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function(){   
var settings = {
  "async": true,
"crossDomain": true,
  "url": "",
  "method" : "GET",
  "headers": {
    "Cache-Control": "no-cache",
 }
}
$.ajax(settings).done(function(data){
    console.log(data.result);
});

这可能是什么原因,我也得到了另一个api的以下错误。

  

否&#39;访问控制 - 允许 - 来源&#39;标题出现在请求的上   资源。起源&#39; http://localhost&#39;因此不允许访问。

0 个答案:

没有答案
相关问题