You should read this : Cors filter and REST Api and CORS filter
Cross-origin resource sharing CORS is a mechanism that allows many resources on a Web page to be requested from another domain outside the domain the resource originated from. In particular, JavaScript's AJAX calls can use the XMLHttpRequest mechanism.
The CORS filter should be defined before Authentication / Authorization filters to make sure the CORS headers will always be sent.
use yii\filters\Cors;
use yii\helpers\ArrayHelper;
public function behaviors()
{
return ArrayHelper::merge([
[
'class' => Cors::className(),
],
], parent::behaviors());
}