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

sl0wik's avatar

Fetching redirect url in authentication with ajax

I'm using standard Laravel authentication with ajax like:

        $.ajax({
            method: "POST",
            url: "/login",
            data: $form.serialize()
        }).done(...);

Successful login attempt wold receive 302 Found + redirect. I cant access redirect. I was trying with "success" or "complete", and all I can get is value after redirect.

How could I fetch redirect url with jQuery?

0 likes
3 replies
tykus's avatar

Your login route will need to return a 2xx JsonResponse (with some content describing where you want to redirect, (e.g. $response()->json(['url'=>'/dashboard'], 200); ) rather that a 3xx, then your AJAX success promise can perform the redirection using window.location(response.data.url)

1 like
sl0wik's avatar

So it cant be done with default Laravel Auth routing?

tykus's avatar

AFAIK, not if you're using AJAX

Please or to participate in this conversation.