laravel 4 持久化数据正在进行 - jquery ajax 提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23536877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:29:54  来源:igfitidea点击:

laravel 4 persisting data ongoing - jquery ajax submit

phpjquerysessionlaravellaravel-4

提问by Ray

I'm having ongoing problems with laravel 4.1 sessions and getting unexpected behaviour.

我在使用 laravel 4.1 会话时遇到了持续的问题并出现了意外行为。

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.

根据我调用控制器方法的方式,会话工作或不工作我的应用程序调用 POST 路由 - 将项目添加到作为会话的购物车中。由于某种原因,会话没有得到更新。但是,如果我使用 GET 请求调用同一个函数,该函数会按预期工作。

My routes.phpcontains these two routes:

我的routes.php包含这两条路线:

Route::get('testAdd', array('uses' => 'ProductsController@addToCart'));

Route::post('products/addToCart', array('uses' => 'ProductsController@addToCart'));

Both point to the same method

两者都指向相同的方法

The method is currently this (for testing):

该方法目前是这样的(用于测试):

public function addToCart() {

  Session::put("addcarttest", "add to cart");
  return json_encode(Session::get('addcarttest'));

}

If I call the function with the POST method (with form data) I get the expected result and the contents of the session.

如果我使用 POST 方法(使用表单数据)调用该函数,我会得到预期的结果和会话的内容。

However If I then check for the session (using a profiler) it does not exist. The data did not persist.

但是,如果我然后检查会话(使用探查器),它就不存在。数据没有持久化。

If I then call the same method using the GET route, I get the expected result but importantly the session persists.

如果我然后使用 GET 路由调用相同的方法,我会得到预期的结果,但重要的是会话仍然存在。

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.

我想也许 POST 方法删除了会话,但是一旦它存在,它就会保留在那里 - 如果我使用 GET 方法并且 sessin 存在,如果我再次尝试 POST 方法示例,会话仍然存在 - 所以 POST 方法不会删除会话。

This is driving me crazy - I've lost a lot of hours over this and can't see why.

这让我发疯 - 我已经为此浪费了很多时间,但不明白为什么。

Am I missing something over how Laravel handles POST v GET ? Why would two different methods make a difference to underlying functions?

我是否遗漏了 Laravel 如何处理 POST v GET 的内容?为什么两种不同的方法会对底层功能产生影响?

What do I need to do to make the session work correctly with POST?

我需要做什么才能使会话与 POST 一起正常工作?

Update:

更新:

I've now tried database driver for the session and am getting the same behaviour.

我现在已经为会话尝试了数据库驱动程序,并且得到了相同的行为。

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.

我已经将我的测试更进一步 - 我创建了一个基本表单并提交到 url 并且该方法按预期工作。我当前的表单数据是由 jquery ajax 提交的,并假设它们在行为上完全相同。

My jquery submit function is this:

我的 jquery 提交功能是这样的:

$.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;

I set async to false - I assume to await the server response. (doesnt work if true either).

我将 async 设置为 false - 我假设等待服务器响应。(如果为真也不起作用)。

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.

所以问题是表单提交和ajax提交之间的细微差别。两种方法都采用相同的路线和方法——一种保存会话数据——另一种不保存。

How can I overcome? Jquery submit is essential to this part of the app.

我怎样才能克服?Jquery 提交对于应用程序的这一部分至关重要。

回答by Ray

Success!

成功!

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.

我发现了一个与 laravel 3 相关的类似问题。为了让会话保持在 ajax 调用中,我需要正确返回响应。

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

This enabled the session to persist!

这使会话能够持久化!

For some reason a normal form submit or call to the method allows the first one but ajax does not.

出于某种原因,普通表单提交或调用该方法允许第一个,但 ajax 不允许。

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

我在其他地方看到过关于影响会话数据的方法中的 echo 语句的参考,但认为我没有任何 - 我认为返回的行为必须类似于 echo

Happy now (till the next problem)

现在快乐(直到下一个问题)

This is the post that triggered the solution: http://forumsarchive.laravel.io/viewtopic.php?id=1304

这是触发解决方案的帖子:http: //forumsarchive.laravel.io/viewtopic.php?id=1304

回答by b0x

i have same issues, but when returning XML Response, not JSON.

我有同样的问题,但返回 XML 响应时,而不是 JSON。

I fixed it using session save.

我使用会话保存修复了它。

\Session::put('LOGADO.ID_LOJA', $id_loja.time());

\Session::save();

This fixed everything inside AJAX Calls.

这修复了 AJAX 调用中的所有内容。

回答by dxhans5

This is in reference to the solution that Ray gave. I had a very similar problem, which your solution resolved. Initially, I had the following on my Controller:

这是参考Ray给出的解决方案。我有一个非常相似的问题,你的解决方案解决了。最初,我的控制器上有以下内容:

echo json_encode(array('auth' => $auth));

I changed this to:

我把这个改成:

return Response::json(array('auth' => $auth));

However, this was only part of the solution. In my javascript file, I initially had:

然而,这只是解决方案的一部分。在我的 javascript 文件中,我最初有:

data = $.parseJSON(data);

Which, if I had kept the echo in the controller...would have been needed. apparently Laravel will do some magic behind the scenes when using Response::json() so that the data that is returned is already parsed. Removing that line in my javascript file made everything happy again :)

其中,如果我将回声保留在控制器中......就会需要。显然 Laravel 在使用 Response::json() 时会在幕后做一些魔术,以便返回的数据已经被解析。删除我的 javascript 文件中的那一行,一切又变得愉快了:)