Why doesn't cookies (Yii2)?

On domain1.com insert js code which sends Ajax request to domain2 , and it set a cookie it will not be installed, or rather installed it again today and I need to set one time and get data in the future

public function getCookieName()
{
 return md5('model_Widget_' . $this->id); 
}

public function getCookie()
{
 $is cookiename = $this->is cookiename;
 $value = null;

 $cookies = Yii::$app->request->cookies;
 if (!$cookies->has($is cookiename)) {
 $value = 'test';
 Yii::$app->response->cookies->add(new \yii\web\Cookie([
 'name' => $is cookiename,
 'value' => $value,
 'expire' => time() + 3600 * 24 * 365 * 10
]));
 } else {
 $value = $cookies->get($is cookiename);
}

 return $value;
 }


the controller is fetching $model = Widget::findOne($id)
next is selecting $model->cookie should return the previously set cookie

ajax
$.ajax({
 url: '//domain2.com/get-widget',
 data: {project: 1},
 type: 'post',
crossDomain:true,
async:true,
 dataType: 'json',
 success: function (data, status) {
 if (data.status == 'ok') {
$('body').append(data.html)
}
}
 })


to the question why the need for cookies - it is necessary to display a widget on certain settings, for example it is necessary to detect first time visitor to the site, and then after a certain time after the first entrance to display this widget
March 12th 20 at 08:41
2 answers
March 12th 20 at 08:43
@marilie.Kuph ajax provided, while both domains locally on my computer , but in the future will be on different servers

p.s. in General the hike in php can not be done, seen a few examples , all done with js cookies

p.p.s and cors I have done has not helped
March 12th 20 at 08:45
Try to change dataType in the ajax request to "jsonp"
$.ajax({
 url: '//domain2.com/get-widget',
 data: {project: 1},
 type: 'post',
crossDomain:true,
async:true,
 dataType: 'jsonp',
 success: function (data, status) {
 if (data.status == 'ok') {
$('body').append(data.html)
}
}
 })

JSONP
Although, I think it's not the best solution.
In Yii2 there is a filter for Cors. Maybe You can help.
https://ru.wikipedia.org/wiki/Cross-origin_resourc...

Find more questions by tags HTTP CookiesYii