php - 返回 HTTP 405 的 CORS 预检请求

标签 php http-headers cors silex

我正在尝试创建一个 RESTful 网络服务,但在实现 PUT 请求时遇到了困难。我已尝试按照本网站上的其他答案和 Mozilla 的各种文章进行操作,但未能成功。

请求是从域 wwwtest.dev-box 生成的,它将转到 test.dev-box(基本上是一个前端应用程序调用后端应用程序)。以下是我从实时 HTTP header 中捕获的 header :

http://test.dev-box/resource/v1/data/user/1

OPTIONS /resource/v1/data/user/1 HTTP/1.1
Host: test.dev-box
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://wwwtest.dev-box
Access-Control-Request-Method: PUT
Connection: keep-alive

HTTP/1.1 405 Method Not Allowed
Date: Wed, 16 Oct 2013 16:15:58 GMT
Server: Apache/2.2.15 (Red Hat)
x-powered-by: PHP/5.3.27
Access-Control-Allow-Origin: http://wwwtest.dev-box
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS
Access-Control-Max-Age: 1728000
Content-Length: 0
Allow: PUT
Cache-Control: no-cache
Connection: close
Content-Type: text/html; charset=UTF-8

我正在使用一个框架,所以所有内容都被路由到 web.php,它在页面顶部包含以下代码(取自 this MDN article):

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header('Access-Control-Allow-Origin: http://wwwtest.dev-box');
    header('Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS');
    header('Access-Control-Max-Age: 1728000');
    header("Content-Length: 0");
    header("Content-Type: text/plain");
} else {
    header("HTTP/1.1 403 Access Forbidden");
    header("Content-Type: text/plain");
}

在我的代码发出 PUT 请求之前,它首先发送 CORS 预检 OPTIONS 请求(正如您从上面的捕获中看到的那样)。必要的 header 应附加到响应中,并告诉请求者允许 PUT。不幸的是,正如您从上面的响应 header 中看到的那样,即使 PUT 在响应 access-control-allow-methods 中,它仍然返回 405 Method Not Allowed。

这是我正在使用的框架 (Silex) 的 .htaccess 文件:

<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ web.php [QSA,L]
</IfModule>

最佳答案

我发现答案是 Apache 和框架配置之间的交叉。

  1. 对于 Apache 配置,您可以将以下内容放入您的 VirtualHost 指令中,或者放入所请求域的 .htaccess 文件中(如果它在 .htaccess 中,请记住使用 IfModule mod_headers.c 标签进行封装)。在 mod_rewrite 重定向到的页面上设置 header 不起作用:
    Header set Access-Control-Allow-Origin "http://wwwtest.dev-box"
    Header set Access-Control-Allow-Methods "GET,POST,HEAD,DELETE,PUT,OPTIONS"
  1. 对于 Silex 配置,将以下内容放入您的应用程序路由中。它基本上会为收到的任何 OPTIONS 请求发送一个 HTTP 200 OK。在 Silex Google Group 上找到的代码:
    $app->match("{url}", function($url) use ($app) {
        return "OK";
    })->assert('url', '.*')->method("OPTIONS");

这两个步骤都需要完成,RESTful 应用程序才能正常运行。

关于php - 返回 HTTP 405 的 CORS 预检请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409105/

相关文章:

php - AJAX使用$_POST方法传输大量数据

php - Laravel 中间件 : Header may not contain more than a single header, 检测到新行

cordova - CORS 和 PhoneGap 应用

azure - 是否可以使用 NancyFX 启用 CORS?

php - 我应该使用 filter_var 来验证电子邮件吗?

php - PHP 与 Javascript 中 MT 实现中相同种子的不同输出

php - 拉拉维尔 5.5 : php artisan tinker : ErrorException : Directory not empty

python - Flask 可以在响应中发送重复的 header 吗?

spring - 如何防止将 header 名称转换为小写 - Spring boot?

javascript - OAuth2 访问源错误