3

The cache of laravel works for me but is the same values in different browsers.

I'm trying to set a session in laravel, and set session variable through ajax call, then in another ajax call get that session variable.

$.ajax({
 type: "post",
 url: url+'setdata',
 data: $('form#data').serialize(),
 dataType: "json",
 processData: false,
 async: false,
 success: function (data) {

  }
});


$.ajax({
 type: "post",
 url: url+'getdata,
 data: $('form#data').serialize(),
 dataType: "json",
 processData: false,
 async: false,
 success: function (data) {

  }
});

In the first ajax request, I call a function from one controller in laravel. This function performs:

Session::put('examplekey', 800);

In the second request, the controller function retrieves the session ID:

$var = Session::get('examplekey');
return array($var);

My problem is, in the second ajax call the session disappears. How can I can set the same session for the same user, in pure php with session_start();

2
  • Hey! Maybe this post will help you find out what's going on: sixdayprogrammer.com/2013/11/… Also, please post more of your PHP Commented Mar 13, 2014 at 15:56
  • I'm experiencing a similar problem - have you been able to resolve? Commented May 8, 2014 at 16:20

1 Answer 1

4

I had the same problem and just found a potential solution:

I found a similar problem relating to laravel 3. For the session to persist in an ajax call you 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. Change it to:

return Response::json($response); 

This enables 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 - the return I suppose must behaving similar to an echo

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

Sign up to request clarification or add additional context in comments.

1 Comment

The link is broken.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.