Refresh/Reload page after Ajax Sucess - Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Correctly Refreshing the Page after Ajax Success in Laravel Projects
In your Laravel project, you want to refresh your page after an Ajax success but are struggling with how to achieve this while maintaining proper performance and security. You've tried redirecting with Laravel's `Redirect` function in the controller and including a refresh within the Ajax call, but nothing seems to work as expected. This blog post will cover the right methodology for refreshing your page after an Ajax success in a Laravel application.
1. **Controller**: Use the Laravel Redirect with Flash Messages for Successful Operations:
In your controller, handle the Ajax response as follows:
if(request()->ajax()) {
//do something
return ['success' => 'successfully done'];
}
Upon successful Ajax completion, you can then redirect and pass a message to the view using Laravel's `Redirect` class:
<script type="text/javascript">
if(request()->ajax()) {
//do something
return ['success' => 'successfully done'];
return redirect('admin/all')->with('status','Successfully done!');
}
</script>
This will redirect the user to your specified route while passing a success message through flash data.
2. **Client-Side JavaScript**: Handle Ajax Success and Redirect with Laravel's Request Token:
When your Ajax call is successful, you can update your page and include a redirect in your JavaScript code using Laravel's `CSRF Token`:
<script type="text/javascript">
$(document).on('confirm', function(e) {
var ele = e.target;
e.preventDefault();
$.ajax({
url: ele.href,
type: 'GET',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success: function (data) {
if(data['success']) {
location="/admin/all";
} else if(data['error']) {
alert(data['error']);
} else {
//Handle unexpected cases
}
},
error: function (data) {
alert(data.responseText);
}
});
return false;
});
</script>
This ensures that your Ajax request is properly authorized and handled, allowing for proper page redirection when necessary.
3. **Avoiding Redirects in JavaScript**: Use Laravel's Confirmation Plugin:
To handle confirmation prompts before redirecting or performing any significant action on the page, you can utilize Laravel's built-in confirmation plugin:
<script type="text/javascript">
$(document).on('confirm', function(e) {
var ele = e.target;
e.preventDefault();
$.ajax({
url: ele.href,
type: 'GET',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success: function (data) {
if(data['success']) {
$("#" + data['tr']).slideUp("slow");
alert(data['success']);
location="/admin/all";
} else if(data['error']) {
alert(data['error']);
} else {
//Handle unexpected cases
}
},
error: function (data) {
alert(data.responseText);
}
});
return false;
});
</script>
This ensures that your users are given a clear option to confirm their intended action and can proceed only after providing confirmation, further enhancing the security of your Laravel application.
In summary, successfully refreshing the page after an Ajax success in your Laravel project involves using Laravel's built-in functionality for Redirect with Flash Messages, handling client-side JavaScript requests effectively, and utilizing Laravel's confirmation plugin to ensure proper user input before redirecting or performing any critical operations. By following these best practices, you can confidently refresh your page while maintaining optimal performance and security in your Laravel application.