laravel 4 persisting data ongoing - jquery ajax submit

前端 未结 3 526
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 07:16

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

Depending on how I call the controllers method the session either works or d

相关标签:
3条回答
  • 2020-12-16 07:48

    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.

    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.

    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)

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

    0 讨论(0)
  • 2020-12-16 08:05

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

    I fixed it using session save.

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

    This fixed everything inside AJAX Calls.

    0 讨论(0)
  • 2020-12-16 08:07

    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:

    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:

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

    0 讨论(0)
提交回复
热议问题