Laravel visits the login page through ajax. How to jump to the page before login after login?

I wrote the method in the login controller:

protected function redirectTo()
    {
        return URL::previous();
    }

but this method can only jump to login pages accessed through web;

if you use ajax to access the login page, jump to the home page after login;

write breakpoints in the method:

protected function redirectTo()
    {
        dd(\request()->headers->get("referer"));
    }

whether accessed through ajax or web, http://laravel.demo.com/login

is returned

Why does it return the url, of the landing page instead of the url? of the previous page?

the referer address is also included in the header of the request for ajax to access the login page. Why is the jump result different when you use ajax and web to access the page?

Mar.06,2021

the first method, history?
the second is to tag the previous page, and then jump to the corresponding page


according to the tag.
when you log in to the page to initiate an ajax request, you get a higher-level page through referer, which must be the login page.

there are two ways to record the original page at this time:

  1. Click the link to jump to the login page with the original page url parameters such as https://www.xxx.com/login?redirect_url=A page url
  2. on the login page, get the previous page through referer,.

after your ajax request is complete, jump back to the original page.

Menu