dtvfshi5248
2014-10-14 08:40 浏览 222
已采纳

Laravel Ajax和Session问题?

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    douyiji3919 douyiji3919 2016-02-23 18:27

    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.

    点赞 评论 复制链接分享
  • duanbeng1923 duanbeng1923 2014-10-14 12:45

    @Broshi Can you please do these 2 things?:

    1. Remove "\" in \Session::get('ajax_fired');.
    2. Use var_dump Session::all() and put exit before \Session::put('ajax_fired',1);.

    Please let me know the result after that. Thanks.

    点赞 评论 复制链接分享

相关推荐