I’m trying to setState a variable by calling a controller action via Javascript (jQuery), so in the .js file I have a single function:
$.post('/main/JSTest');
In the controller, the JSTest action is:
public function actionJSTest()
{
Yii::app()->user->setState('JS', 1);
}
Looking at the session data, the ‘JS’ variable is not being set, but if I add the setState into the normal processing outside of the javascript call, it does get set.
Any reason the call to the controller action from JS doesn’t setState but calling setState during normal processing of any controller action does? I was guessing the session data is available to the app anytime any controller action is called.
Thanks
Edit - I should mention that the user has already authenticated at the time JSTest is called.
I took out the redirect after the JSTest action runs and it is in fact setting the JS flag to 1. It loses it after the redirect for some reason, like the session is not the same. I know there are issues with secure and insecure sessions, but the variable is being set after login, in an open session for use in other non-SSL pages.
For whatever reason, the variable does not persist, but other session variables I assign during login (in UserIdentity) are persistent. What’s the difference?
I had to add an extra step and renderPartial a page with a noscript refresh and a regular script block with the ajax call. It gives about a half second of white screen between login and initial authenticated page but at least I have a state variable I can branch with to render either html only or js-enabled widgets.
Ok, so I do the redirect on ajax complete which means i have one less function to worry about, but there’s still the additional page in the middle to do the actual call (and thus test for JS).
So right now it’s:
Login/Auth
Redirect to JSTest view which has the ajax call and the noscript refresh
Ajax call fires and sets JS to 1 or JS stays 0 and refresh occurs
Is that as simple as it can be? Or is there really a way to avoid that page in the middle?