Laravel Ajax login, redirect to previous url after success Laravel Ajax login, redirect to previous url after success laravel laravel

Laravel Ajax login, redirect to previous url after success


I have a very similar problem here: Ajax Auth redirect on Laravel 5.6

As @aimme (https://stackoverflow.com/users/1409707/aimme) pointed out, Ajax calls are stateless, so basically you can't interact with backend.

His suggestion and my suggestion is to pass in the URL the intended page to redirect to, or maybe in your case you could to it via post parameters, e.g.:

return response()->json([    'auth' => false,    'intended' => $request->intended,    'errors' => $validator->errors()]);


It might help you

Instead of these return redirect()->intended(URL::route('dashboard'));

use

return redirect('dashboard');


There is no need to do anything special for AJAX calls.

Redirect the same way you normally would on the back-end after a form submission.

return redirect()->route('dashboard');

On the front-end you just need to be sure that you use the redirected URL to change the window.location. This will cause the browser to refresh and go to the new page.

axios.post(url, formData).then(response => {    window.location = response.request.responseURL;});

This code snippet is for the popular Axios library but the same thing can be done with jQuery or vanilla JavaScript.