CakePhp 2.4 API CORS问题

时间:2019-05-27 12:23:20

标签: php .htaccess cakephp cross-domain

当尝试从另一台主机访问API时,蛋糕php上出现跨源问题。我已经尝试过这里给出的多种解决方案,但是它们都不起作用。我尝试过:

Htaccess:

Header always set Access-Control-Max-Age 1728000
Header always set Access-Control-Allow-Origin: "*"
Header always set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header always set Access-Control-Allow-Headers: "x-rmr-api-key,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,C$"

RewriteEngine on
RewriteRule    ^$    webroot/    [L]
RewriteRule    (.*) webroot/$1    [L]

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']}");

}

AppController.php

if ($this->request->is('options')) {
    return $this->response;
}

我还能做什么?

错误返回的响应可以在此处查看:request/response details

0 个答案:

没有答案