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

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?

图片转代码服务由CSDN问答提供 功能建议

我有一个ajax调用,当DOM完成加载时会激活,但我试图防止出现这种情况 用户点击刷新并通过存储会话变量再次触发ajax调用。 但是,通过AJAX请求存储会话条目似乎存在问题,请考虑以下片段:

  &lt;?php 
function postMyAjax(){
 
 $ already_fired = \ Session :: get('ajax_fired');  //&lt;  - 总是返回NULL!
if(!empty($ already_fired)){
返回Response :: json(array('have fired')); 
} 
 
#记住我们解雇了 调用
 \ Session :: put('ajax_fired',1);  //&lt;  - 无法设置?
 
return Response :: json(array('ok')); 
 
} 
   
 
 

任何 想法?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 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 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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题