Symfony的,jQuery.ajax()调用,会话变量丢失变量、Symfony、jQuery、ajax

2023-09-10 17:30:50 作者:長野

我写使用的Symfony 1.3的web应用程序。我有一个问题,我的会话变量迷路我打电话,通过jQuery的一个动作后, $。阿贾克斯()通话。事情发生在顺序如下:

这是动作设置这样的会话变量:

  $这个 - >的getUser() - >的setAttribute(上传档案,$ UPLOADEDFILES);
 

然后,当操作调用视图组件,生成的HTML页面包含以下电话:

  $。阿贾克斯({
  键入:POST,
  网址:'< PHP的回声url_for(内容/合并); ?>',
  缓存:假的,
  成功:mergingCompleteCallback
});
 
jQuery Ajax

我点击触发上述Ajax调用按钮。呼叫执行相应的操作方法,但是当我打印出来的上传档案会话变量的内容,它是空的。

我也检查,看看是否会话ID停留在调用该设置变量的页面,并且读取变量(Ajax调用)和会话ID没有改变的页面之间的相同。我在几个小时在网上搜索,我无法找到一个解决方案。

解决方案

我有类似的问题,这个问题是,我使用的主机值存储饼干WWW,即www.mydomain.com,我是做没有Ajax请求的 WWW

解决方案是使用像主机通配符。 例如 .mydomain.com 作为主机来存储会话cookie的浏览器端。

只要确定你是不是这样的受害者?

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:

An action sets the session variable like this:

$this->getUser()->setAttribute('uploaded-files', $uploadedFiles);

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
});

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.

解决方案

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?