User identity missing with an Ajax call

Hi,

I’m facing a strange problem: user identity are missing when an action is called by ajax, it’s considered as a guest.

I have this problem in Chrome, not in Firefox :mellow:

HELP!!!

With what? :)

Show us some code, give us a sign …

in my JS file:

$.ajax({

type: ‘POST’,

url: ‘../advertisement/checkremainclicks’,

dataType: ‘json’,

data: {id: idAd},

success: function(data) {

Action

public function actionValidateclicks(){

	if (Yii::$app->request->isAjax){


		if(Yii::$app->user->isGuest)


			return 1;


		$visit = new Visit();


		$mem = Yii::$app->user->identity->login;


		$ad = $_POST['id'];


		


		$visit->member_login = $mem;


		$visit->advertisement_idadvertisement = $ad;


		$visit->ip = Yii::$app->request->userIP;

It returns 1 on chrome, but do the action on firefox

I tried also an absolute URL…

Try and read this:

http://www.mushtaqtahir.com/blog/3/yii2-ajax-form-submission

Thank you a lot for the effort!

But this issue is not getting from a form, here is an example

is user logged in both browsers??

login detection is based on session and cookie.

so make sure that user is logged in chrome too.

I’m logged on both

Other members facing same issue.

try this in user component config:

‘user’ => [

        'class' => 'YOUR\USER\CLASS',


        'identityCookie'=>['name' => '_identity'],

]

and clear all cookies then try again

Probelm solved! :lol:

I changed




onclick="window.open('<?= 'http://www.starbux.biz/visit/'.$model->idadvertisement ?>','_blank');">



with




onclick="window.open('<?= Url::to(['visit', 'ad' => $model->idadvertisement]) ?>','_blank');">



Seems that chrome omit SESSION when using absolute URL

Thank you all!

I think it happened because you open site with

http://starbux.biz

url and link in ajax request with http://www.starbux.biz,

the cookie will set for domain without www and when you try to access domain with www prefix there is no cookie for session there.

Not at all!

I’m surfing with ‘www’

I even tried before to find the solution the cross domain property on JQuery, but didn’t solve it.

Use db base session that is better.so no user identity problem will come.