Код: Выделить всё
Access to XMLHttpRequest at 'http://rest.site/api/users/login' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
useHttp.js:9
AxiosError {message: 'Network Error', name: 'AxiosError', code: 'ERR_NETWORK', config: {…}, request: XMLHttpRequest, …}
xhr.js:220
POST http://rest.site/api/users/login net::ERR_FAILED
Код: Выделить всё
const axiosAuthorization= (data) => {
return axios.post('http://rest.site/api/users/login', data).then(res => {
console.log(res.data);
}).catch(err => {
console.log(err);
})
};
Код urlmanager
Код: Выделить всё
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => \yii\rest\UrlRule::class,
'controller' => ['api/user'],
'extraPatterns' => [
'POST /' => 'login',
'POST login'=>'login',
],
],
'/' => 'site/index',
'gii' => 'gii',
],
],
Код: Выделить всё
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['corsFilter'] = [
'class' => \yii\filters\Cors::className(),
'cors' => [
'Origin' => ['*'],
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
'Access-Control-Request-Headers' => ['*'],
],
];
$behaviors['contentNegotiator'] = [
'class' => \yii\filters\ContentNegotiator::className(),
'formats' => [
'application/json' => \yii\web\Response::FORMAT_JSON,
],
];
return $behaviors;
}
/**
* @throws \yii\base\InvalidConfigException
* @throws \yii\base\Exception
*/
public function actionLogin()
{
return 'test';
$model = new LoginForm();
if ($model->load(Yii::$app->request->getBodyParams(), '') && $model->loginApi()) {
return 'ok';
return $model->loginApi();
} else {
return $model->getErrors();
}
}
}