php 当被 AJAX 调用时,Laravel 重定向到从控制器路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21180919/
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
Laravel redirect to route from controller when called by AJAX
提问by Jared Eitnier
Given this chunk of ajaxthat calls FriendsController@destroy
鉴于这块ajax电话FriendsController@destroy
$.ajax({
url: '/dashboard/friends/' + id,
type: 'DELETE',
data: { src: 'show' },
success: function(response) {
}
});
How can I return Redirect::route('dashboard.friends.index')inside FriendsControllerafter the delete procedure is completed? I guess this is trying to return the response back to AJAX which doesn't know how to react.
删除程序完成后我如何才能return Redirect::route('dashboard.friends.index')进去FriendsController?我想这是试图将响应返回给不知道如何反应的 AJAX。
I could just window.location.href = '/dashboard/friends'but I want to Flash a success message to the view which I can't do with AJAX.
我可以,window.location.href = '/dashboard/friends'但我想向视图发送一条成功消息,而我无法使用 AJAX。
采纳答案by Jared Eitnier
Here is how I solved this issueon my own. For functions such as performing an AJAX delete of a model, it will prompt a confirmation modal, perform the delete, flash the message and redirect to the provided route.
这是我自己解决这个问题的方法。对于执行 AJAX 删除模型等功能,它将提示确认模式、执行删除、闪烁消息并重定向到提供的路由。
Once setup it can be done dynamically by passing in the proper dataattributes to the original delete button or link.
设置完成后,可以通过将适当的data属性传递给原始删除按钮或链接来动态完成。
回答by don
I found the most easiest way of doing this. just use same url again in your ajax success. assuming friends is your route name then
我找到了最简单的方法来做到这一点。只需在 ajax 成功中再次使用相同的 url。假设朋友是你的路线名称然后
$.ajax({
url: '/dashboard/friends/' + id,
type: 'DELETE',
data: { src: 'show' },
success: function(response) {
window.location.href = "friends";
}
});
this way you can just put redirect or flash session message if you want.
通过这种方式,您可以根据需要放置重定向或 Flash 会话消息。
回答by Ahmed Aboud
i know this might be old question but if you want to return a dynamic route
我知道这可能是个老问题,但如果您想返回动态路线
return route('post.view', ['id' => $result]);
return route('post.view', ['id' => $result]);
and in your front-end you will do
在你的前端你会做
success: function(response){
window.location.replace(response);
}
回答by naghal
I came across the same problem and here is what did the tricks for me.
我遇到了同样的问题,这是对我有用的技巧。
In your controller, you can flash the message using:
在您的控制器中,您可以使用以下方法闪烁消息:
\Session::flash('success', __('locale.message'));
\Session::flash('success', __('locale.message'));
And then in your ajax response handler, you redirect to wherever you want
然后在你的 ajax 响应处理程序中,你重定向到你想要的任何地方
location.replace('/home');
location.replace('/home');
Don't forget to add the login in your view to display the message, I.E:
不要忘记在您的视图中添加登录以显示消息,IE:
@if (session('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('success') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endif
回答by user1062954
if(data.success){
var loc = window.location;
window.location = loc.protocol+"//"+loc.hostname+"/admin/dashboard";
}

