You can not echo directly nor redirect inside ajax call. What you need is to echo or redirect using javascript/jquery inside ajax callback
So e.g. in controller you have
public function destroy($id) {
$category = Category::find($id);
$category->delete();
return 'success'; //not the best value to return, but for illustration :)
}
Then in jquery. This will alert the 'success' string from controller
$.ajax({
type: "DELETE",
url: "{{ URL::to('admin/categories/" +idtoedit +"')}}",
success: function (result) {
alert(result);
}
});
If you want to redirect, you must do it in jquery too
$.ajax({
type: "DELETE",
url: "{{ URL::to('admin/categories/" +idtoedit +"')}}",
success: function (result) {
window.location.replace('/admin/categories');
}
});
Thanks! It works fine!! Only one change needed: window.location.replace('categories'); instead of 'admin/categories'.
It worked fine in Laravel 4, but I'm trying it in Laravel 5 and doesn't work. I replaced and {{ }} with {!! !!}
url: "{!! URL::to('admin/categories/" +idtoedit +"')!!}",
This also does not work!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community