Po This question mam ustawione mego odpoczynku zachowanie kontrolera jakoYii2 Cors filtruje błąd, który nie 'Access-Control-Allow-Origin' header jest obecny
public function behaviors()
{
$behaviors = parent::behaviors();
$auth= $behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
'only' => ['dashboard'],
];
$behaviors['contentNegotiator'] = [
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
];
$acces=$behaviors['access'] = [
'class' => AccessControl::className(),
'only' => ['login'],
'rules' => [
[
'actions' => ['login'],
'allow' => true,
'roles' => ['?'],
],
],
];
unset($behaviors['authenticator']);
unset($behaviors['access']);
A teraz filtry Cors
// add CORS filter
$behaviors['corsFilter'] = [
'class' => \yii\filters\Cors::className(),
'cors' => [
// restrict access to
'Access-Control-Allow-Origin' => ['*'],
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
// Allow only POST and PUT methods
'Access-Control-Request-Headers' => ['*'],
// Allow only headers 'X-Wsse'
'Access-Control-Allow-Credentials' => true,
// Allow OPTIONS caching
'Access-Control-Max-Age' => 86400,
// Allow the X-Pagination-Current-Page header to be exposed to the browser.
'Access-Control-Expose-Headers' => [],
]
];
// re-add authentication filter
$behaviors['authenticator'] = $auth;
$behaviors['access'] = $access;
// avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method)
$behaviors['authenticator']['except'] = ['options'];
return $behaviors;
}
Nakładka moja angular2 jako Ale nadal otrzymuję komunikat o błędzie
Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested resource. Origin
'http://localhost:3000' is therefore not allowed access.
Co może być nie tak, ponieważ ive ustawić filtr Cors zachowań yii2 rozbrojony uwierzytelniający i dodaje go później Co mogę być brakuje
Mam również sprawdzony na This link a także this one ale żaden rozwiązuje kwestia
'Access-Control-Request-Headers' jest na stronie klienta, po stronie serwera należy użyć' Access- Control-Allow-Headers' – particleflux
próbował dodać Access-control-allow-headers ['*'], ale nadal nie działa –
Spróbuj zastąpić "Access-Control-Allow-Origin" => ['*'], z "Origin" => ['*'], –