Session :: Flash在Ajax Laravel 5中不起作用 [英] Session::flash not working in ajax laravel 5

查看:91
本文介绍了Session :: Flash在Ajax Laravel 5中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为getuser的动作,被ajax调用.

I have action named getuser which is called by ajax.

这是我的ajax

var $_token = $('#token').val();
var BASE = "{{ URL::to('/') }}";
$.ajax({
        url: BASE + "/admin/user/test",
        type: 'POST',
        data: {type:type, id:id},
        headers: { 'X-XSRF-TOKEN' : $_token },
        success: function(data){
            // Success...
            console.log(data);
            location.reload();
        }
    }); 

控制器动作

public function getuser()
{
    Session::flash('message', 'This is a message!'); 
    Session::flash('alert-class', 'alert-danger');
    exit;
}

进入视图,我有这样的代码

And into view i have code like this,

@if(Session::has('message'))
    <p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ Session::get('message') }}</p>
@endif

但不能为我工作.有什么建议吗?

But not working for me. Any suggestion?

推荐答案

您可以在ajax请求中刷新会话,但是这不会更改当前视图.
如果要在ajax请求后更新当前页面,则必须使用javascript回调和javascript代码来执行此操作.

You can flash a session in your ajax request, hower this will not change the current view.
If you want to update the current page after an ajax request you will have to use a javascript callback and javascript code to do this.

请阅读这篇文章有关客户端脚本和服务器端脚本之间的区别.

Please read this post about the difference between client side and server side scripting.

更新:
您将永远不会收到在ajax请求中设置的刷新值.刷新的会话变量只能持续一个请求.因此,当操作返回并且输出返回给ajax请求时,将清除闪烁的变量.

Update:
You will never receive the flashed values set in your ajax request. Flashed session variables only last for one request. So when the action returns and the output is send back the the ajax request, the flashed variables are cleared.

解决此问题的最佳方法是不使用ajax或使用实际的会话变量并手动清除它们.

The best way to solve this is to not use ajax or use actual session variables and clear them manualy.

这篇关于Session :: Flash在Ajax Laravel 5中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆