REST_CONTROLLER cors无法正常工作(Codeigniter) [英] REST_CONTROLLER cors not working (Codeigniter)

查看:100
本文介绍了REST_CONTROLLER cors无法正常工作(Codeigniter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,因为这个问题困扰着我好几天,而且我不知道如何继续.我正在使用VueJs2和Codeigniter(Rest控制器)作为我的API开发我的应用程序. 我使用的是节点Web Pack服务器(npm run dev),因此我的首页应用程序在 http://localhost:8080 .该应用正在向我的Codeigniter应用发出请求(我正在使用nginx虚拟主机,因此我可以在 http://myapp.test )

I need some help, because this problem is bugging me for days and i don't know how to proceed further. I'm developing my application with VueJs2 and Codeigniter (Rest controller) as my API. I'm using node web-pack server (npm run dev) so my front page app runs on http://localhost:8080. This app is making requests to my Codeigniter application (I'm using nginx virtual host, so i can access my apis on my http://myapp.test)

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

The problem is following. My VueJs application is making GET request from http://localhost:8080, with some custom header

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配置

And this is my rest.php configuration regarding CORS

$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',
];

这是我的控制者,以获取用户

And this is my controller, to get user

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

此请求的结果为405不允许使用方法(因为请求的方法为OPTIONS(我假设这是飞行前请求),所以失败了)

And the result of this request is 405 Method not allowed (because requested method is OPTIONS (i assume that this is pre-flight request), which is failing)

因此,假设CORS出了点问题,我已经使用与您在上面看到的相同的设置启用了它,但是问题仍然存在.

So assuming that something is wrong with CORS i have enable it with all the same setting as u can see above, but problem still remains.

$config['check_cors'] = TRUE;

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

The only difference i see is, that response header now allow all methods and headers, but get request doesn't execute

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

这是我针对php的nginx配置

This is my nginx configuration for php

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行,则我的代码正在运行".首先发出选项请求(失败),然后发出获得请求的请求

If i make my controller to look like this and if i remove add_header lines from nginx.conf, then my code "is working". First makes options request (which is failing), then it makes get request which is ok

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

有人可以帮帮我吗?我不知道我还需要做什么,这样我就可以从 http://localhost:8080 请求资源a>到我的 http://myapp.test API

Can some one please help me out? I don't know what else do i have to do so i will be able to request resources from http://localhost:8080 to my http://myapp.test API

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

If you need any additional informations, please let me know and i will provide. Thank you!

推荐答案

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

For fixing the CORS problem, add that to your 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的响应.

Because you need to react to ALL OPTIONS requests, not only to index_options.

这篇关于REST_CONTROLLER cors无法正常工作(Codeigniter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆