Symfony, jQuery.ajax() call, session variables lost

前提是你 提交于 2019-12-06 04:36:21

问题


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.


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/7232701/symfony-jquery-ajax-call-session-variables-lost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!