Yii2 cors filters error that No 'Access-Control-Allow-Origin' header is present

后端 未结 4 1340
粉色の甜心
粉色の甜心 2020-12-17 10:22

Following This question i have set my rest controller behaviour as

public function behaviors()
{
    $behaviors = parent::behaviors();

    $auth= $behaviors         


        
4条回答
  •  醉酒成梦
    2020-12-17 10:29

    Try This :

    public static function allowedDomains()
    {
        return [
            // '*',                        // star allows all domains
            'http://localhost:3000',
            'http://test2.example.com',
        ];
    }  
    
    
    
    public function behaviors()
        {
            return array_merge(parent::behaviors(), [
    
                // For cross-domain AJAX request
                'corsFilter'  => [
                    'class' => \yii\filters\Cors::className(),
                    'cors'  => [
                        // restrict access to domains:
                        'Origin'                           => static::allowedDomains(),
                        'Access-Control-Request-Method'    => ['POST'],
                        'Access-Control-Allow-Credentials' => true,
                        'Access-Control-Max-Age'           => 3600,                 // Cache (seconds)
    
                    ],
                ],
    
            ]);
        }
    

    Add This Function on your controller .

    And One Thing angular2 use OPTION method at the first time For so allow OPTION method also

提交回复
热议问题