Refresh/Reload page after Ajax Sucess - Laravel

Refresh/Reload page after Ajax Sucess - Laravel

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Refresh/Reload Page After Ajax Success in Laravel

When building dynamic admin panels, there's a specific friction point where developers often get stuck: handling the user experience immediately after an asynchronous operation completes. You want to show a success message, maybe slide up the row to indicate deletion, and then return the user to their main dashboard. The instinct is to mix server-side redirection with client-side JavaScript logic, but doing so incorrectly leads to the exact deadlock described in common troubleshooting threads.

The core issue usually stems from a mismatch between what the controller returns and what the frontend expects. In your scenario, you are checking if (request()->ajax()) in the controller. The problem lies in the subsequent lines. You cannot simply return a JSON array and then immediately append a redirect response object to it. In PHP, once you hit a return, the function exits. The code snippet showing both return ['success' => ...] and return redirect(...) is logically impossible; only one will execute.

If your goal is to refresh the page entirely after an AJAX success, you should return a standard HTTP 302 redirect response from the controller. This forces the browser to load the new URL. However, if you are handling the logic purely on the client side via JavaScript, relying on location.href inside the .success() callback is often overkill and can feel jarring because it ignores the partial DOM updates (like sliding up the row) that happened before the reload.

Here is a robust pattern that separates concerns cleanly. The controller handles the state change and redirects only if necessary, or returns JSON for frontend handling. If you want the page to reload automatically after a successful AJAX call without a manual redirect in the JS, let the controller handle the response type based on the request header.

// Controller Example
public function approve()
{
    // Validate input and perform action
    $ids = request('ids'); 
    // ... logic to process IDs

    if (request()->ajax()) {
        // For AJAX requests, return JSON. Do not redirect here unless you want a full page load.
        return response()->json([
            'success' => 'Row deleted successfully.',
            // Optionally pass the ID of the row to hide or slide up
            'tr_id' => $this->getTrId($ids) 
        ]);
    }

    // For non-AJAX requests (e.g., direct navigation), redirect as normal
    return redirect('/admin/all')->with('status', 'Successfully done!');
}

On the frontend, you need to decide whether to refresh the whole page or just update the DOM. If you choose to refresh the entire page, the JavaScript in the .success() callback is responsible for triggering that navigation. However, a common pitfall is placing location.href immediately after sliding up an element. While this works, it discards all state changes made via AJAX and forces a full HTTP request cycle.

If you prefer to keep the data fresh without a full reload, remove the location.href line and instead clear your local UI state. For example:

success: function (data) {
    if (data['success']) { 
        // Update DOM
        $("#" + data['tr']).slideUp("slow");
        
        // Clear selection to prevent accidental re-submission
        $('.sub_chk').prop('checked', false); 
        
        // Optional: Scroll to top or focus another element for UX
        $('html, body').animate({ scrollTop: 0 }, 'slow');

        // No need to reload the page here unless you specifically want a full refresh
    } 
}

There is a specific edge case where you might actually want a full redirect from the AJAX response. If your controller returns redirect('admin/all'), the browser will ignore the JSON content and navigate immediately. In this case, your frontend code inside the .success() callback becomes irrelevant because the page has already left that URL.

To avoid this confusion, ensure your controller strictly returns JSON when request()->ajax() is true. If you need to force a reload regardless of whether it was an AJAX call or not, you can append a redirect response at the very end of the script execution in the view, but this is generally considered an anti-pattern for AJAX flows because it breaks the asynchronous nature of the request.

A cleaner approach for a full page refresh after success is to simply return the redirect from the controller and let the browser handle the navigation naturally. If you must handle it client-side, use window.location.href but ensure no other code runs afterwards. The key takeaway is consistency: either the server decides the destination (redirect) or the client decides based on the response data (JS navigation). Mixing both without a clear conditional check leads to the behavior where the page refreshes at the wrong time or not at all.

Our network