laravel 4持久化数据正在进行中-jQuery Ajax提交 [英] laravel 4 persisting data ongoing - jquery ajax submit

查看:108
本文介绍了laravel 4持久化数据正在进行中-jQuery Ajax提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

laravel 4.1会话一直存在问题,并且出现了意外的行为.

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

根据我如何调用controllers方法,会话可以工作还是不起作用我的应用程序会调用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.

我已经在其他地方看到了有关影响会话数据的方法中的回显语句的参考,但我认为我没有任何意义-我认为返回的内容必须类似于回显

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提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆