Laravel被AJAX调用时从控制器重定向到路由 [英] Laravel redirect to route from controller when called by AJAX

查看:40
本文介绍了Laravel被AJAX调用时从控制器重定向到路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这段调用 FriendsController @ destroy

$.ajax({
    url:     '/dashboard/friends/' + id,
    type:    'DELETE',
    data:    { src: 'show' },
    success: function(response) {
    }
});

在删除过程完成后,如何在 FriendsController 返回Redirect :: route('dashboard.friends.index')?我想这是试图将响应返回给不知道如何反应的AJAX.

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.

我可以只是 window.location.href ='/dashboard/friends',但我想将成功消息闪现到我无法使用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.

推荐答案

这是我如何解决此问题自己的.对于执行模型的AJAX删除之类的功能,它将提示确认模式,执行删除,闪烁消息并重定向到提供的路径.

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.

一旦设置,可以通过将适当的 data 属性传递到原始的删除按钮或链接来动态完成.

Once setup it can be done dynamically by passing in the proper data attributes to the original delete button or link.

这篇关于Laravel被AJAX调用时从控制器重定向到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆