5

I am writing a web app using Symfony 1.3. I have a problem with my session variables getting lost after I call an action via jQuery's $.ajax() call. Things happen in this order:

  1. An action sets the session variable like this:

    $this->getUser()->setAttribute('uploaded-files', $uploadedFiles);
    
  2. Then when the action calls the view component, the resulting HTML page contains the following call:

    $.ajax({
      type: "POST",
      url: '<?php echo url_for("content/merge"); ?>',
      cache: false,
      success: mergingCompleteCallback
    });
    
  3. I click on the button that triggers the above ajax call. The call executes the corresponding action method, but when I print out the content of the 'uploaded-files' session variable, it's empty.

    I also checked to see if the session id stays the same between the call to the page that sets the variable and the page that reads the variable (the ajax call) and the session id hasn't changed. I was searching for hours online and I wasn't able to find a solution.

6
  • 2
    How are you printing out the contents, and what are the contents? As ajax requests use the same session cookie as any other request, there is no reason why your session variables would get lost over Ajax and not over regular requests. Commented Aug 29, 2011 at 16:42
  • Hi. The AJAX call just returns a path to a file via a callback. The URL request calls an action that calls a system call pdftk(some parameters) and then returns a callback with the path to a file name. The problem that I am experiencing is that the action located at url_for("content/merge") doesn't see the session variable ($this->getUser()->getAttribute('attr_name');). However, when I run this command by directly typing the URL into the browser, thereby not using AJAX, the session variable gets loaded properly. That's what baffles me. I expected the same behavior that you described. Thanks! Commented Sep 6, 2011 at 14:24
  • 1
    maybe you session cookie is httponly, is this case jquery can't send it. Commented Jun 20, 2012 at 17:03
  • Does url_for("content/merge") return a relative url or absolute? If absolute, try reducing it down to relative. Commented Aug 11, 2012 at 18:25
  • in your post request I see no uploaded-files variable, where do you set it ? check headers of XFR request to be sure that ajax send them as expected. Commented Aug 14, 2012 at 3:20

2 Answers 2

3
+50

I had the similar problem, and the issue was that I was using host value for storing cookies with 'www' i.e. www.mydomain.com and I was making Ajax request without 'www'

The solution is use the wildcard like host. Like .mydomain.com as your host to store session cookie on browser end.

Just make sure you are not a victim of this?

Sign up to request clarification or add additional context in comments.

1 Comment

that's dont solve my problem! i dont know why u earned the Bounty but thank you for answer!
1

You could also add the session values to the call itself, that way you don't have to get it in the AJAX call. Its a bit hacky, but it will work.

Do you see the session variable in the dev bar? Maybe its caching, I've had a caching problem like this before.

1 Comment

The global session Variable is only shows after reload the page. The User Session is show too but is lock. I can't change values that are into User Session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.