Php 飞行前请求不';无法通过访问控制检查

Php 飞行前请求不';无法通过访问控制检查,php,angular,rest,cors,Php,Angular,Rest,Cors,试图从angular 8 web应用程序发出JWT授权的json rest api请求,但出现上述错误。所有合理的CORS配置步骤都已经遵循,然后放松规则,直到它仍然不起作用,我已经向你们寻求帮助 就CORS配置而言,在我开始使用的php方面: header("Access-Control-Allow-Origin: http://localhost:4200"); header("Content-Type: application/json;charset=utf-8"); header("A

试图从angular 8 web应用程序发出JWT授权的json rest api请求,但出现上述错误。所有合理的CORS配置步骤都已经遵循,然后放松规则,直到它仍然不起作用,我已经向你们寻求帮助

就CORS配置而言,在我开始使用的php方面:

header("Access-Control-Allow-Origin: http://localhost:4200");
header("Content-Type: application/json;charset=utf-8");
header("Access-Control-Allow-Methods: OPTIONS, GET, PUT, POST, DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
我稍微放松了一些,以便:

header("Access-Control-Allow-Origin: *");
header("Content-Type: *");
header("Access-Control-Allow-Methods: *");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: *");
但后来我在CORS文档中读到,Access Control Allow标头不允许*,所以我还原了这一行

目前我有:

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json, text/plain, */*");
header("Access-Control-Allow-Methods: OPTIONS, GET, PUT, POST, DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, X-Requested-With, Authorization");
我得到的错误是:

访问位于的XMLHttpRequest "来源" “”已被CORS策略阻止:响应 飞行前请求未通过访问控制检查:它没有 HTTP ok状态

这是来自调试工具(Chrome)中网络选项卡的请求的标题信息

临时标题显示为Accept:application/json、text/plain、, /授权:持票人EYJ0EXAIIOIJKV1QILCHbGCIOIJIUZI1NIJ9。修订

这是在带有PHP7.2的apache上运行的

我希望新的轻松CORS设置能够工作,但我仍然会遇到这个错误


如何调整CORS设置以满足飞行前的请求?

我创建了一个名为的框架。这是我用来克服CORS精神错乱的方法

    $requestMethod = $_SERVER['REQUEST_METHOD'];

    // Is this a pre-flight request (the request method is OPTIONS)? Then start output buffering.
    if ($requestMethod === 'OPTIONS') {
        ob_start();
    }

    // Allow for all origins and credentials. Also allow GET, POST, PATCH, and OPTIONS request verbs
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, OPTIONS, DELETE');

    // If this is a pre-flight request (the request method is OPTIONS)? Then flush the output buffer and exit.
    if ($requestMethod === 'OPTIONS') {
        ob_end_flush();
        exit();
    }

HTH

检查兄弟开发人员工具中的网络选项卡。您应该看到一个带有方法“OPTIONS”的请求,即飞行前请求。从上面的错误看来,这个请求没有得到HTTP ok响应这正是答案。我将非常感兴趣地研究您的Willow框架,但目前输出缓冲解决方案的工作方式非常有魅力。非常感谢你。