REST_CONTROLLER cors 不工作(Codeigniter)

Val*_*or_ 0 php rest codeigniter nginx node.js

我需要一些帮助,因为这个问题困扰了我好几天,我不知道如何进一步处理。我正在使用 VueJs2 和 Codeigniter(Rest 控制器)作为我的 API 开发我的应用程序。我正在使用节点 web-pack 服务器(npm run dev),所以我的首页应用程序在http://localhost:8080 上运行。此应用程序正在向我的 Codeigniter 应用程序发出请求(我使用的是 nginx 虚拟主机,因此我可以在http://myapp.test上访问我的 api )

问题如下。我的 VueJs 应用程序正在从http://localhost:8080发出 GET 请求,并带有一些自定义标头

this.$http.get(this.$apiUrl + `rest/api/public/User/user/` + payload.id_user, {
    // if i remove this custom header, everything works ok!
    headers: {
        jwttoken: token
    }
})
Run Code Online (Sandbox Code Playgroud)

这是我关于 CORS 的 rest.php 配置

$config['check_cors'] = FALSE;
$config['allowed_cors_headers'] = [
    'Origin',
    'X-Requested-With',
    'Content-Type',
    'Accept',
    'Jwt-token',
    'jwttoken'
];
$config['allow_any_cors_domain'] = TRUE;
$config['allowed_cors_origins'] = [
    'http://localhost:8080',
];
Run Code Online (Sandbox Code Playgroud)

这是我的控制器,让用户

class User extends REST_Controller {
   public function user_get($id) {
        $this->response([
            'status' => true,
            'data'   => 'data'
        ], REST_Controller::HTTP_OK);
    }
}
Run Code Online (Sandbox Code Playgroud)

而这个请求的结果是 405 Method not allowed (因为请求的方法是 OPTIONS (我假设这是飞行前请求),这是失败的) 在此处输入图片说明

因此,假设 CORS 出现问题,我已使用您在上面看到的所有相同设置启用它,但问题仍然存在。

$config['check_cors'] = TRUE;
Run Code Online (Sandbox Code Playgroud)

我看到的唯一区别是,响应标头现在允许所有方法和标头,但 get 请求不执行

Access-Control-Allow-Headers:*
Access-Control-Allow-Methods:*
Access-Control-Allow-Origin:*
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这是我对 php 的 nginx 配置

location /rest {
    index /rest/index.php;
    try_files $uri $uri/ /rest/index.php;

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, PATCH, DELETE';
    add_header 'Access-Control-Allow-Headers' '*';
}

location ~ \.php$ {
    try_files        $uri =404;
    fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
    fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param    WORKING_ENVIRONMENT dev;
    include          fastcgi_params;
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, PATCH, DELETE';
    add_header 'Access-Control-Allow-Headers' '*';
}
Run Code Online (Sandbox Code Playgroud)

如果我让我的控制器看起来像这样,并且如果我从 nginx.conf 中删除 add_header 行,那么我的代码“正在运行”。首先发出选项请求(失败),然后发出获取请求,这是可以的

class User extends REST_Controller {
    public function user_get($id) {
        $this->getUser();
    }
    public function user_options() {
        $this->getUser();
    }
    private function getUser($id) {
        var_export($id);
        var_dump('test');
        die();
    }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

有人可以帮我吗?我不知道我还需要做什么,所以我将能够从http://localhost:8080向我的http://myapp.test API请求资源

如果您需要任何其他信息,请告诉我,我会提供。谢谢!

Val*_*or_ 6

要解决 CORS 问题,请将其添加到您的 Rest-Controller

class My_Rest_controller extends REST_Controller
{
    public function __construct($config = 'rest')
    {
        parent::__construct($config);

        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因为您需要对所有 OPTIONS 请求做出反应,而不仅仅是对 index_options。