Try using the Session facade instead of doing $requets->session().
LARAVEL: AJAX request dont set session variable
Hello everyone! I am trying to set up session variable by AJAX request in the Laravel. It is simple but it seems I miss something. My page has in layout cookies agreement box. After clicking to the "I agree with cookies" button within this box, AJAX request goes on- here in controller it shall set up session variable. If this session variable is set then, it shall hide always the cookies agreement box.
My ajax request on frontend:
<script>
jQuery(document).ready(function(){
jQuery('#accept_cookies_btn').click(function(e){
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
jQuery.ajax({
url: "{{ url('/acceptcookies') }}",
method: 'post',
data: {
},
success: function(){
$("#cookies_con").hide();
}});
});
});
</script>
Controller:
class cookiesController extends Controller
{
public function acceptcookies(Request $request){
$request->session()->put('cookies', 'agreed');
return 'success';
}
}
PS: Response of AJAX is good, since element #cookies_con hide after doing so. Basically the problem is it wont set up the session variable (because if I browse later, this session variable is not set- i.e. i cannot use it as a condition to hide the cookies agreement box).
PS2: All other is in order.
Thanks for any help, i believe i am missing just some detail. Carpe diem.
Please or to participate in this conversation.