Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

halloei's avatar

Get url of redirected page when posting with ajax

I'm viewing a product in my online shop. The url looks like example.com/#product/1. To add the product, I'm posting the form via ajax to the add method of my CartController (see below). This method then redirects to the shopping cart, so the user can review it. My problem is, in that way the url is not updated. For the cart it should be example.com/#cart.

I tried to add middleware which modifies the headers, but this all won't work. It would be great if the php code for that could also be used when not using ajax.

Does someone have a clue?

    public function add(Request $request) {
        // add the product to cart
        return redirect('shop/cart');
    }

    public function show(Request $request) {
        // get all products from cart
        return view('shop.cart', compact('cart'));
    }
0 likes
5 replies
Snapey's avatar

If you are just going to redirect, why even bother with Ajax?

But to answer your question, you cannot redirect an ajax request, without the client detecting what was returned and reloading the page client side. But as I say, all pointless.

halloei's avatar

The header and navigation of my page are only loaded once. After that, all content is loaded within the content-div. That's why the routes are faked with an anchor.

I thought the response of my show method could hold a header 'redirected' => 'shop/cart' that I can fetch with jQuery to update the url. But I can't get that to work.

Currently I'm using a theme which uses Ajax. But preferably my php code should also work with a theme that doesn't use Ajax.

athithan's avatar

handling the redirection on client side is one way,

for example,

return a success response when add operation is successful,

return response()->json([
    'status' => 'success'
], 201);

and then capture the response of the ajax request, redirect to cart page

$.post(url, {data}, function(response){
    if (response.status == 'success') location.href = '/cart';
});
halloei's avatar

Thanks, but I wanted a solution which was as abstract as possible. Today, back to work, I've ended up with this:

I've "overwritten" Laravel's helper redirect():

if(!function_exists('redirect')) {
    function redirect($to = null, $status = 302, $headers = [], $secure = null) {
        if(is_null($to)) {
            return app('redirect');
        }

        $response = app('redirect')->to($to, $status, $headers, $secure);

        return $response->with('Redirected-To', $to);
    }
}

..and a new middleware:

public function handle($request, Closure $next) {
    $response = $next($request);

    if(($redirectedTo = session('Redirected-To'))) {
        $response->header('Redirected-To', $redirectedTo);
    }

    return $response;
}

Then in jQuery's success function:

if(jqXHR.getResponseHeader('Redirected-To') !== null) {
    history.pushState(null, null, '#'+jqXHR.getResponseHeader('Redirected-To'));
}

Works like charm!

Please or to participate in this conversation.