Laravel redirect to route from controller when called by AJAX
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
When working on web applications built using the Laravel framework, it's crucial to effectively handle different types of user requests. One such request is an asynchronous JavaScript and XML (AJAX) request that often involves manipulating data without reloading the page. In this article, we will learn how to redirect from a controller inside your Laravel application when called by AJAX to achieve successful communication between the client-side code and the server.
The Problem
You want to delete a friend record in your application after receiving a DELETE request sent via AJAX. Once this action is completed, you need to return a specific route for displaying friends list on your dashboard. You are also looking for a way to show a success message on the corresponding view. However, since AJAX operates asynchronously, it doesn't receive the response from the server directly.
A Possible Solution
$.ajax({
url: '/dashboard/friends/' + id,
type: 'DELETE',
data: { src: 'show' },
success: function(response) {
window.location.href = '/dashboard/friends';
}
});
One way to solve this problem is by redirecting the user back to friends list page using JavaScript after performing the delete operation. While you can achieve this, you won't be able to display a custom success message since it will be a server-side redirection.
The Best Solution with Laravel
$.ajax({
url: '/dashboard/friends/' + id,
type: 'DELETE',
data: { src: 'show' },
success: function(response) {
let redirectRoute = "";
$.ajax({
url: redirectRoute,
method: 'GET',
dataType: 'json',
success: response => {
const html = `Friend deleted successfully.`; // Custom success message
$("#friends_list").html(html);
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('Error deleting friend.', jqXHR.responseJSON);
}
});
By using the above code snippet, you can efficiently handle AJAX requests in your Laravel application. This process involves two AJAX calls: one for performing the delete action and another to redirect to a specific route while displaying the success message on the client side.
Conclusion
In conclusion, handling asynchronous requests from AJAX in your Laravel application is an essential skill that allows you to create more responsive and efficient web applications. By understanding how to redirect from controllers using AJAX and displaying a custom success message on the client side, you can improve the user experience within your Laravel-based projects.
Note: Remember that whenever you make server calls via JavaScript, use jQuery.ajax, or if you prefer, fetch, as browsers may support native XMLHttpRequest differently. This ensures cross-browser compatibility and easier management for your Laravel application.