Many times 401 "Unauthorized". by ajax requests

one of my application pages is loading some content via ajax on my page (currently 2 requests, after the document is ready). Many times I get for this ajax requests the status "401" with the answer "Unauthorized". Sometimes when I refresh the page (with F5), it works, sometimes one request gets the status 401. And the less time I get 500 (laravel in this case uses the wrong database credentials, not from .env).

Can someone help me with these problems?

Using Laravel 5.1.6

thanks

public function handle($request, Closure $next) { if ($this->auth->guest()) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { return redirect()->guest('auth/login'); } } return $next($request); } 
0
source share
2 answers

You can try using '|| $ request-> wants Json () 'with if to check if the request is ajax.

 if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('auth/login'); } 
0
source

This is related to your login session. Whenever your session expires. Request response "Unauthorized".

0
source

Source: https://habr.com/ru/post/1234705/


All Articles