REST_CONTROLLER cors无法正常工作(Codeigniter)

问题描述 投票:0回答:1

我需要一些帮助,因为这个问题困扰了我好几天,我不知道如何继续前进。我正在用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
    }
})

这是我关于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',
];

这是我的控制器,以获得用户

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

并且此请求的结果是405方法不允许(因为请求的方法是OPTIONS(我假设这是飞行前请求),这是失败的)enter image description here

因此,假设CORS出现了问题,我可以使用与上面所见相同的设置启用它,但问题仍然存在。

$config['check_cors'] = TRUE;

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

Access-Control-Allow-Headers:*
Access-Control-Allow-Methods:*
Access-Control-Allow-Origin:*

enter image description here

这是我的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' '*';
}

如果我使我的控制器看起来像这样,如果我从nginx.conf中删除add_header行,那么我的代码“正在工作”。首先发出选项请求(失败),然后使得get请求正常

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();
    }

enter image description here

有人可以帮帮我吗?我不知道还有什么我必须这样做,我将能够从http://localhost:8080请求资源到我的http://myapp.test API

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

php node.js rest codeigniter nginx
1个回答
1
投票

要解决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();
        }
    }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.