87

我已经使用 SLIM PHP 框架创建了一个基本的 RESTful 服务,现在我正在尝试将它连接起来,以便我可以从 Angular.js 项目访问该服务。我已经读到 Angular 支持开箱即用的 CORS,我需要做的就是将这一行添加Header set Access-Control-Allow-Origin "*"到我的 .htaccess 文件中。

我已经这样做了,我的 REST 应用程序仍在工作(没有来自错误的 .htaccess 的 500 内部服务器错误),但是当我尝试从test-cors.org对其进行测试时,它抛出了一个错误。

Fired XHR event: loadstart
Fired XHR event: readystatechange
Fired XHR event: error

XHR status: 0
XHR status text: 
Fired XHR event: loadend

我的 .htaccess 文件看起来像这样

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [QSA,L]
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"

我还需要在我的 .htaccess 中添加其他内容以使其正常工作,还是有其他方法可以在我的服务器上启用 CORS?

4

8 回答 8

103

因为无论如何我都将所有内容转发到 index.php,所以我想我会尝试在 PHP 中设置标头而不是 .htaccess 文件,它确实有效!耶!这是我为遇到此问题的其他人添加到 index.php 的内容。

// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
    // should do a check here to match $_SERVER['HTTP_ORIGIN'] to a
    // whitelist of safe domains
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");         

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

}

归功于 slashingweapon 对这个问题的回答

因为我使用的是 Slim,所以我添加了这条路由,以便 OPTIONS 请求获得 HTTP 200 响应

// return HTTP 200 for HTTP OPTIONS requests
$app->map('/:x+', function($x) {
    http_response_code(200);
})->via('OPTIONS');
于 2013-01-22T22:44:07.033 回答
95

不应该.htaccess使用add代替set吗?

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
于 2013-12-30T11:20:41.263 回答
34

这对我有用:

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
于 2016-02-06T05:43:06.943 回答
14

如在this answer Custom HTTP Header for a specific file中,您可以使用<File>以下代码为单个文件启用CORS:

<Files "index.php">
  Header set Access-Control-Allow-Origin "*"
  Header set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
</Files>

而不是"*"您可以放置​​特定的来源(协议+域+可选端口)。

于 2017-03-18T11:32:45.340 回答
13

将 100% 工作,在 .htaccess 中应用:

# Enable cross domain access control
SetEnvIf Origin "^http(s)?://(.+\.)?(1xyz\.com|2xyz\.com)$" REQUEST_ORIGIN=$0
Header always set Access-Control-Allow-Origin %{REQUEST_ORIGIN}e env=REQUEST_ORIGIN
Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "x-test-header, Origin, X-Requested-With, Content-Type, Accept"

# Force to request 200 for options
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule .* / [R=200,L]
于 2018-11-27T01:16:35.913 回答
9

看起来您正在使用旧版本的 slim(2.x)。您只需将以下行添加到 .htaccess 中,而无需在 PHP 脚本中执行任何操作。

# Enable cross domain access control
SetEnvIf Origin "^http(s)?://(.+\.)?(domain_one\.com|domain_two\.net)$" REQUEST_ORIGIN=$0
Header always set Access-Control-Allow-Origin %{REQUEST_ORIGIN}e env=REQUEST_ORIGIN
Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE"
Header always set Access-Control-Allow-Headers: Authorization

# Force to request 200 for options
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule .* / [R=200,L]
于 2016-02-29T18:35:10.103 回答
5

感谢 Devin,我为我的 SLIM 应用程序找到了多域访问的解决方案。

在 htaccess 中:

SetEnvIf Origin "http(s)?://(www\.)?(allowed.domain.one|allowed.domain.two)$" AccessControlAllowOrigin=$0$1
Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header set Access-Control-Allow-Credentials true

在 index.php 中

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");         

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
}
// instead of mapping:
$app->options('/(:x+)', function() use ($app) {
    //...return correct headers...
    $app->response->setStatus(200);
});
于 2014-10-27T14:26:36.143 回答
1

我尝试了@abimelex 解决方案,但在 Slim 3.0 中,映射 OPTIONS 请求如下:

$app = new \Slim\App();
$app->options('/books/{id}', function ($request, $response, $args) {
    // Return response headers
});

https://www.slimframework.com/docs/objects/router.html#options-route

于 2017-01-12T20:42:03.237 回答