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.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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'));
}
Please or to participate in this conversation.