laravel 如何在ajax中访问laravel flash数据/消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39385365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access laravel flash data/message in ajax?
提问by Ayaz Shah
I'm trying to access laravel flash messages in the ajax request. I searched about it there are some solutions available, but i want to show them without reloading the page.
我正在尝试访问 ajax 请求中的 laravel flash 消息。我搜索了它有一些可用的解决方案,但我想在不重新加载页面的情况下显示它们。
What I'm Trying :
我在尝试什么:
In my controller i'm setting flash message
在我的控制器中,我正在设置 flash 消息
Session::flash('success', 'Record has been inserted successfully');
And
和
return response()->json([
'success' => 'ok' // for status 200
]);
In Ajax
在阿贾克斯
success: function(data){
if(data.success){
// reload the current page
window.location.href = 'http://localhost/Framework/augLaravel3/public/index.php/user/add';
}
}
In View
在视图中
@if (Session::has("success"))
{{ Session::get("success") }}
@endif
Above code is working properly, i just want to remove the reloading step, because i'm think it will be more convenient for complex or big application if i show the message through ajax (without page reload). But as i'm new to laravel so i don't know can i convert session::flash
to json or not?
上面的代码工作正常,我只想删除重新加载步骤,因为我认为如果我通过ajax显示消息(不重新加载页面),对于复杂或大型应用程序会更方便。但是因为我是 laravel 的新手所以我不知道我是否可以转换session::flash
为 json ?
Can anyone guide me about this, that its possible? and it will be correct way if we access session::flash
in ajax? I would like to appreciate if someone guide me. Thank You.
任何人都可以指导我,这可能吗?如果我们session::flash
在ajax中访问它会是正确的方式吗?如果有人指导我,我将不胜感激。谢谢你。
回答by maelga
Instead of hardcoding html with the server's response like @Abbbas khan proposes, I would rather return a sub-view for flash messages and have the possibility to reuse that sub-view on any page.
不是像@Abbbas khan 建议的那样使用服务器的响应对 html 进行硬编码,我宁愿为 flash 消息返回一个子视图,并有可能在任何页面上重用该子视图。
1. Create your flash messages sub-view:Create partials/flash-messages.blade.php
1. 创建您的 flash 消息子视图:创建 partials/flash-messages.blade.php
@if( Session::has("success") )
<div class="alert alert-success alert-block" role="alert">
<button class="close" data-dismiss="alert"></button>
{{ Session::get("success") }}
</div>
@endif
//Bonus: you can also use this subview for your error, warning, or info messages
@if( Session::has("error") )
<div class="alert alert-danger alert-block" role="alert">
<button class="close" data-dismiss="alert"></button>
{{ Session::get("error") }}
</div>
@endif
2. In your controller:Define the success message you want and return your flash-messages sub-view
2. 在你的控制器中:定义你想要的成功信息并返回你的 flash-messages 子视图
use Session;
use View;
Session::flash('success', 'File has been uploaded successfully!');
return View::make('partials/flash-messages');
3. In your main view:Place a empty div at the location you'd like jquery to append your flash message
3. 在您的主视图中:在您希望 jquery 附加您的 flash 消息的位置放置一个空的 div
<div class="flash-message"></div>
4. In your ajax code:Pass your sub-view as a html response and attach it to the div defined above
4.在您的ajax代码中:将您的子视图作为html响应传递并将其附加到上面定义的div
$.ajax({
...
dataType: 'html', //Optional: type of data returned from server
success: function(data) {
$('div.flash-message').html(data);
},
...
});
5. Why I like this approach:With this server-side approach, you have your flash messages ready to be reused on any page. Your ajax code is also streamlined and you can also simply reuse it again on another page. The only thing you need to customise is the message content that you neatly define in your controller! Awesome!
5. 为什么我喜欢这种方法:使用这种服务器端方法,您可以在任何页面上重复使用您的 Flash 消息。您的 ajax 代码也得到了简化,您也可以简单地在另一个页面上再次使用它。您唯一需要自定义的是您在控制器中巧妙定义的消息内容!惊人的!
回答by Abbbas khan
if ($Validator->fails()) {
return Response::json([
'message'=>'you have not inserted',
'error' =>$Validator->errors()->toArray()
]);
}
else{
$this->flight->name = $request->name;
$this->flight->detail = $request->detail;
$this->flight->save();
return Response::json([
'message'=>'you have inserted']);
}
in ajax as
success: function(data){
if(data.error){
$('#main').html('<div class="alert alert-danger col-ssm-12" >' + data.message + '</div>');
$('#nameid').html('<div class="alert alert-danger col-sm-12" >' + data.error.name + '</div>');
$('#detailid').html('<div class="alert alert-danger col-sm-12">' + data.error.detail + '</div>');
// location.reload('/');
}
else{
$('#main').html('<div class="alert alert-info col-ssm-12" >' + data.message + '</div>');
}
}