Reputation: 31
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();
Upvotes: 3
Views: 6899
Reputation: 3060
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
Upvotes: 4