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

Wakanda's avatar
Level 10

How to redirect a page after an ajax call

Hi Devs,

I am trying to redirect to a route after an ajax call but the am not getting a redirect

my controller

 public function pollTransaction(Request $request)
    {
        $this->validate($request, [
            'transaction' => 'exists:orders,id'
        ]);

        // Find a transaction matching the given transaction id
        $transaction = Order::findOrFail($request->input('transaction'));

        try {
            // Try to poll the transaction
            // $status = $this->paynow->pollTransaction($transaction->poll_url);
            $status = $this->paynow->pollTransaction($transaction->token);

            if ($status->paid()) {
                 // Update the transaction of the transaction
                $transaction->is_paid = true;

                // Persist the new status
                $transaction->save();
    

                session()->flash('success', "Thank you $transaction->billing_fullname, your payment has been received successfully!");

                if (auth()->check()) {
                    return redirect(route('my-orders.index'));  /// am trying to redirect here
                } else{
                    return redirect(route('thank.you'));
                }
            }
        } catch (Exception $e) {
            // Log out the error
            logger()->error($e->getMessage() . "\t\t" . $e->getTraceAsString());

            return response()->json([
                'status' => 'error',
                'message' => 'An error occurred while polling transaction'
            ]);
        }
    }

AJAX

(function () {
			let poll = function() {
				$.ajax({
					"_token": "{{ csrf_token() }}",
					url: '{!! route('pollTransaction') !!}',
					method: "GET",
					data : {
						transaction : {!! $newTransaction['transaction']['id'] !!},
					},
					contentType: false,
					cache: false,
					proccesData: false,
					dataType: "json",
					success:function(data) {
						console.log(data)
					}
				})
			}

			setInterval(function() {
				poll()
			}, 10000);
		})();

without ajax the redirect works perfectly

0 likes
8 replies
alanholmes's avatar

Hi @loyd

If you do a redirect within an ajax call, it will be the ajax call itself that redirects, not the calling page.

Instead, what you will need to do, is have the ajax call return the URL to redirect to, and then have the Javascript do the redirect for you.

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102
success:function(data) {
						window.location = data.url
					}

//and in php
return response()->json(['url' => route('my-orders.index')]);
1 like
Wakanda's avatar
Level 10

@sinnbeck thanks for replying I also need to show the success message on redirect how can I append that message

Snapey's avatar

just tell me, why are you using ajax for this and not a regular form pst ?

1 like
Wakanda's avatar
Level 10

@snapey @sinnbeck I am polling a transaction from the payment gateway to get the status of the transaction when a user enters a pin code from their mobile. This is after a user has clicked an order or paynow button, as soon as they place and confirm the order a USSD transaction is sent to the user mobile to perform the transaction so at this point I want to automatically end the transaction without having to ask the client or user to click a button to confirm that they have entered a pin.

Wakanda's avatar
Level 10

@snapey so basically the ajax poll is in the loop of the payment gateway subsequent request and it requires automation

Please or to participate in this conversation.