Фронт на Vue.js. Пытаюсь отправить post или delete - ошибка CORS.
Отправляю GET - все нормально, что за магия????? Как мне POST отправить??
Access to XMLHttpRequest at 'http://192.168.99.101:8000/api/v1/items/7' from origin 'http://127.0.0.1:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Рекомендации, которые помогли GET запросам заработать:
public function beforeAction($action)
{
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['corsFilter'] = [
'class' => \yii\filters\Cors::class,
'cors' => [
// restrict access to
'Origin' => ['*'],
// Allow methods
'Access-Control-Request-Method' => ['POST', 'PUT', 'OPTIONS', 'GET', 'DELETE'],
// Allow only headers 'X-Wsse'
'Access-Control-Request-Headers' => ['*'],
'Access-Control-Allow-Headers' => ['Content-Type'],
// 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' => ['*']
],
];
return $behaviors;
}
РЕШЕНИЕ:
Спасибо всем и
Rsa97 за наводки, далее этот пост помог решить проблему с CORS Preflight:
https://coderoad.ru/36274089/Yii2-CORS-с-Auth-не-р...