问题描述
我在使用 laravel 4.1 会话时遇到问题并出现意外行为.
I'm having ongoing problems with laravel 4.1 sessions and getting unexpected behaviour.
根据我调用控制器方法的方式,会话是否有效我的应用程序会调用 POST 路由 - 将项目添加到作为会话的购物车中.由于某种原因,会话没有得到更新.但是,如果我使用 GET 请求调用同一函数,则该函数按预期工作.
Depending on how I call the controllers method the session either works or doesn't My app makes a call to a POST route - to add items to a cart which is a session. For some reason the session does not get updated.However if I make a call to the same function with a GET request the function works as expected.
我的 routes.php
包含这两条路由:
My routes.php
contains these two routes:
Route::get('testAdd', array('uses' => 'ProductsController@addToCart'));
Route::post('products/addToCart', array('uses' => 'ProductsController@addToCart'));
两者都指向同一个方法
方法目前是这样的(测试用):
The method is currently this (for testing):
public function addToCart() {
Session::put("addcarttest", "add to cart");
return json_encode(Session::get('addcarttest'));
}
如果我使用 POST 方法(使用表单数据)调用函数,我会得到预期的结果和会话内容.
If I call the function with the POST method (with form data) I get the expected result and the contents of the session.
但是,如果我随后检查会话(使用分析器)它不存在.数据没有持久化.
However If I then check for the session (using a profiler) it does not exist. The data did not persist.
如果我随后使用 GET 路由调用相同的方法,我会得到预期的结果,但重要的是会话仍然存在.
If I then call the same method using the GET route, I get the expected result but importantly the session persists.
我认为 POST 方法可能会删除会话,但是一旦它存在,它就会保留在那里 - 如果我使用 GET 方法并且 sessin 存在,如果我再次尝试 POST 方法示例,则会话仍然存在 - 所以 POST 方法不会删除会议.
I thought maybe the POST method deleted sessions however once it exists it stays there - if I use the GET method and the sessin exists if I then try the POST method example again the session remains in place - so the POST method doesnt delete the session.
这让我发疯 - 我为此浪费了很多时间,但不知道为什么.
This is driving me crazy - I've lost a lot of hours over this and can't see why.
我是否错过了 Laravel 如何处理 POST v GET 的内容?为什么两种不同的方法会对底层功能产生影响?
Am I missing something over how Laravel handles POST v GET ? Why would two different methods make a difference to underlying functions?
我需要做什么才能使会话与 POST 一起正常工作?
What do I need to do to make the session work correctly with POST?
更新:
我现在已经尝试了会话的数据库驱动程序并且得到了相同的行为.
I've now tried database driver for the session and am getting the same behaviour.
我已经进一步测试了一个阶段 - 我创建了一个基本表单并提交到 url 并且该方法按预期工作.我当前的表单数据是由 jquery ajax 提交的,并假设它们在行为上完全相同.
I've taken my test a stage further- I created a basic form and submitted to the url and the method worked as expected. My current form data is submitted by jquery ajax and assumed they were fairly identical in behviour.
我的 jquery 提交函数是这样的:
My jquery submit function is this:
$.ajax({
url: '/products/addToCart',
type: 'POST',
async: false,
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
return false;
我将 async 设置为 false - 我假设等待服务器响应.(如果为 true 也不起作用).
I set async to false - I assume to await the server response. (doesnt work if true either).
所以问题是表单提交和 ajax 提交之间的细微差别.两种方法都采用相同的路线和方法——一种保存会话数据——另一种不保存.
So the problem is a subtle difference between a form submit and an ajax submit. Both methods are hitting the same route and method - one saves the session data - the other one doesnt.
我该如何克服?Jquery 提交对于应用程序的这一部分是必不可少的.
How can I overcome? Jquery submit is essential to this part of the app.
推荐答案
成功!
我发现了一个与 laravel 3 相关的类似问题.为了使会话在 ajax 调用中持续存在,我需要正确返回响应.
I found a similar problem relating to laravel 3. For the session to persist in an ajax call I need to return the response correctly.
return json_encode($response);
这是导致问题的原因.这似乎不是使会话持续存在的有效响应.我改成:
This is causing the problem. It's not it appears a valid response to enable the session to persist. I changed it to:
return Response::json($response);
这使会话得以持续!
由于某种原因,普通表单提交或调用方法允许第一个,但 ajax 不允许.
For some reason a normal form submit or call to the method allows the first one but ajax does not.
我在其他地方看到了有关影响会话数据的方法中的 echo 语句的参考,但我认为我没有任何参考 - 我认为返回的行为必须类似于 echo
I've seen references elsewhere about echo statements in the method affecting the session data but did not think I had any - the return I suppose must behaving similar to an echo
现在快乐(直到下一个问题)
Happy now (till the next problem)
这是触发解决方案的帖子:http://forumsarchive.laravel.io/viewtopic.php?id=1304
This is the post that triggered the solution: http://forumsarchive.laravel.io/viewtopic.php?id=1304
这篇关于laravel 4持续数据正在进行 - jquery ajax提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!