2021-04-01 23:50:40 2129次浏览 4条回答 0 悬赏 10 金钱

根据类参考手册 https://www.yiichina.com/doc/api/2.0/yii-filters-cors
设置了 corsFilter,但是依然无效,浏览器显示 CORS missing allow origin

两台电脑都是 basic 模板,A 电脑调用 B 电脑 API 的。

public function behaviors()
{
    return [
        'corsFilter'=>[
            'class' => Cors::className(),
            'cors' => [
                // restrict access to
                'Origin' => ['http://www.abc.com', 'http://account.abc.com'],
                // Allow only POST and PUT methods
                'Access-Control-Request-Method' => ['POST'],
                // Allow only headers 'X-Wsse'
                 'Access-Control-Request-Headers' => ['X-Wsse'],
                // Allow credentials (cookies, authorization headers, etc.) to be exposed to the browser
                'Access-Control-Allow-Credentials' => true,
                // Allow OPTIONS caching
                'Access-Control-Max-Age' => 3600,
                // Allow the X-Pagination-Current-Page header to be exposed to the browser.
                'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
            ],
        ],
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => [],// 此处内容过多,发布到yiichina.com时我删掉了
                    'allow' => true,
                ],
                [
                    'actions' => [],// 此处内容过多,发布到yiichina.com时我删掉了
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => [],// 此处内容过多,发布到yiichina.com时我删掉了
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'signout' => ['post'],
                // 'checkid' => ['post'],
            ],
        ],
        
    ];
}
补充于 2021-04-01 23:51

同时也有 accessverbs 应该不影响吧?

  • 回答于 2021-04-02 09:45 举报

    我也遇到过没有解决 直接在index.php写的 就没有研究为什么了

    1 条回复
    回复于 2021-04-02 13:22 回复

    唉……我并不是整个项目都允许跨站访问的

  • 回答于 2021-04-02 09:50 举报
    • Make sure to read carefully what CORS does and does not. CORS do not secure your API,
    • but allow the developer to grant access to third party code (ajax calls from external domain).
    1 条回复
    回复于 2021-04-02 12:22 回复

    就是同站退出的访问和个人信息的ajax请求。退出功能是写在account.abc.com/signout里,如果在www.abc.com域名下请求了退出,难道还要跳转到account.abc.com里访问一个页面显示正在退出么?我想在www.abc.com直接退出,刷新页面,通过ajax请求完成。真要yiichina.com解决不了我就只能退出跳转account的一个页面,然后显示正在退出登录了……哎

  • 回答于 2021-04-02 10:07 举报

    请上代码

    1 条回复
    回复于 2021-04-02 12:18 回复

    我修改了问题,请帮我看一下哪里有问题。

  • 回答于 2021-04-06 10:33 举报

    https://www.yiichina.com/topic/9325
    不知道是一类问题不

    1 条回复
    回复于 2021-04-21 12:53 回复

    我自己单独搞了个rest模块搞好了,我说的是不用restful控制器,直接继承yii\web\Controller实现跨域,发现不行。

    觉得很赞
您需要登录后才可以回答。登录 | 立即注册
醉挽清风
见习主管

醉挽清风 南京

注册时间:2018-07-03
最后登录:2022-09-06
在线时长:15小时38分
  • 粉丝1
  • 金钱180
  • 威望10
  • 积分430

热门问题