Laravel redirect to route from controller when called by AJAX
Given this chunk of ajax
that calls 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 FriendsController
after the delete procedure is completed? I guess this is trying to return the response back to AJAX which doesn't know how to react.
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.
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: '/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.
Here is how I solved this issue on 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.
Once setup it can be done dynamically by passing in the proper data
attributes to the original delete button or link.
if(data.success){
var loc = window.location;
window.location = loc.protocol+"//"+loc.hostname+"/admin/dashboard";
}
链接地址: http://www.djcxy.com/p/18550.html
上一篇: 面向对象的Codeigniter模型方法