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.

j0k
  • 22,600
  • 28
  • 79
  • 90
pkout
  • 6,430
  • 2
  • 45
  • 55
  • 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. – Gerry Aug 29 '11 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! – pkout Sep 06 '11 at 14:24
  • 1
    maybe you session cookie is httponly, is this case jquery can't send it. – Visavì Jun 20 '12 at 17:03
  • Does `url_for("content/merge")` return a relative url or absolute? If absolute, try reducing it down to relative. – Beetroot-Beetroot Aug 11 '12 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. – zb' Aug 14 '12 at 03:20
  • Please could you put the code of the method that you are calling ( content/merge ) and where you set the attribute as well as var_dump of session before set attribute and var_dump of session in method. Looks like Symonfy dind't initialize the session with your call – Rubén Fanjul Estrada Aug 15 '12 at 09:21

2 Answers2

3

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?

Humayun
  • 976
  • 7
  • 13
  • that's dont solve my problem! i dont know why u earned the Bounty but thank you for answer! – 3logy Aug 27 '12 at 11:22
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.

Arazu
  • 421
  • 2
  • 5
  • 15
  • 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. – 3logy Aug 15 '12 at 08:42