Reputation: 3592
I have an ajax call which fires right when the DOM has finished loading, yet I am trying to prevent a situation when the user hits refresh and fires that ajax call again by storing a session variable. Yet, there seems to be an issue with storing a session entry via AJAX request, consider the following snippet:
<?php
function postMyAjax() {
$already_fired = \Session::get('ajax_fired'); // <-- Always returns NULL!
if ( ! empty( $already_fired ) ) {
return Response::json(array('already fired'));
}
# Remember that we fired that call
\Session::put('ajax_fired',1); // <-- Fails to set?
return Response::json(array('ok'));
}
Any ideas?
Upvotes: 3
Views: 1464
Reputation: 794
For the sake of answering the question, this has been a bug since laravel 4.1 where session handling becomes unstable with asynchronous requests.It is intensively being discussed here in github issues. My research points to the fact that basically, Laravel saves sessions in the last part of the request cycle and ajax/asynchronous requests processes differently as opposed to requests from browsers.
Upvotes: 2