在 ajax 请求 Laravel 后显示 flash 和错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33692466/
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
display flash and error message after ajax request Laravel
提问by Juan Carlo F. Yarra
After a successful ajax request, I want to send a flash message to my view (For example, upon editing, I'd like to redirect the user to the homepage with $flash = "Your shop has been update" ). Within the controller, it is easy but I don't know what to do within JavaScript. Do any of you know how to figure it out? Im using Laravel
在成功的 ajax 请求之后,我想向我的视图发送一条 flash 消息(例如,在编辑时,我想使用 $flash = "Your shop has been update" 将用户重定向到主页)。在控制器中,这很容易,但我不知道在 JavaScript 中该做什么。你们中有人知道如何弄清楚吗?我使用 Laravel
Controller
控制器
public function postUpdate (Request $request)
{
$this->validate($request, [
'website_name' => 'required',
'website_url' => 'required',
'category' => 'required',
'type' => 'required',
'sells' => 'required',
'location' => 'required',
'description' => 'required',
'payment' => 'required'
]);
Shop::where('username', '=', Auth::user()->username)->update(['website_name' => Input::get('website_name'),
'website_url' => Input::get('website_url'), 'type' => Input::get('type'), 'category' => Input::get('category'), 'sells' => Input::get('sells'), 'location' => Input::get('location'),
'payment' => Input::get('payment'), 'description' => Input::get('description')]);
return Response::json(['message' => 'Success', 'message_class' => 'alert alert-success fade in']);
}
AJAX
AJAX
$(".update-form").submit(function(s){
s.preventDefault();
var website_name = $('input[name=website_name]').val();
var website_url = $('input[name=website_url]').val();
var type = $('#type option:selected').val();
var category = $('#category option:selected').val();
var sells = $('input[name=sells]').val();
var location = $('input[name=location]').val();
var payment = $('input[name=payment]').val();
var description = $("textarea#message").val();
$.ajax({
type: "POST",
url: "advertiser/update",
data: {
_token: token, website_name: website_name, website_url: website_url, type: type, category: category, sells: sells, location: location, payment: payment, description: description
},
success: function() {
$('.modal-backdrop').remove();
$('body').load('advertiser')
},
error: function(data) {
$('body').load('advertiser')
}
})
});
HTML
HTML
<div class="row" id="errors">
@if (Session::has('message'))
<div class="{!! Session::get('message_class') !!}">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Note!</strong> {!! Session::get('message') !!}
</div>
@endif
@if($errors->has())
<div class="alert alert-danger fade in">
<a href="#" class="close" data-dismiss="alert">×</a>
<p>the following errors have occured:</p>
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
采纳答案by Sulthan Allaudeen
You can't redirect the within controller if you use ajax request.
如果使用 ajax 请求,则无法重定向内部控制器。
But you can do like this.
但是你可以这样做。
Send some parameters in the Controller like this
像这样在控制器中发送一些参数
$Response = array(
'success' => '1',
);
or
或者
$Response = array(
'success' => '0',
'error' => 'Your Flash Message'
);
and return it return $Response;
并归还 return $Response;
Then, In the ajax result you can redirect the user like this
然后,在 ajax 结果中,您可以像这样重定向用户
if (data.success == 1){
window.location = 'toyourdesiredpath';
}
else
{
//show your error message in your div or span
}
回答by Paul Basenko
Just set a session flash message and a redirect url in Laravel controller (or return null if you want to reload the current page) like this:
只需在 Laravel 控制器中设置一个会话 flash 消息和一个重定向 URL(如果你想重新加载当前页面,则返回 null),如下所示:
if ($redirect) {
session()->flash('success', 'Your Flash Message for redirect');
return '/redirect-url';
} else {
session()->flash('success', 'Your Flash Message for reload');
return null;
}
And then make the redirect or reload inside the JavaScript code:
然后在 JavaScript 代码中进行重定向或重新加载:
$.post(`/ajax-url`, $(e.target).serialize()
).done((redirect) => {
if (redirect) {
window.location.href = redirect
} else {
window.location.reload()
}
}).fail((errors) => {
this.setState({
errors: errors.responseJSON.errors
})
})
回答by Harsh Sanghani
You have to use redirect with message, try following example in your code :-
您必须对消息使用重定向,请在代码中尝试以下示例:-
return Redirect::to('user/login')->with('message', 'Login Failed');
return Redirect::to('user/login')->with('message', '登录失败');