How to solve the 302 redirection error when laravel passport uses javascript to call the interface?

use laravel passport as api authentication. A valid token has been applied for using the authorization code.

works well in postman :

clipboard.png

javascript

clipboard.png

clipboard.png

javascript :

clipboard.png

I said that I am particularly speechless, why postman using token is normal, and then javascript uses token access but authentication is not successful for redirection!

Mar.21,2021

when a request contains a custom header (sometimes Authorization is also considered a custom header), the browser will initiate a preflight request in the form of options before initiating the real request. Laravel does not handle the pre-check request correctly, so it returns 404, and then the web server redirects 404 to the error page.

the following solution is not very elegant:
is to add middleware that handles preflight .

protected $middleware = [
    \App\Http\Middleware\PreflightResponse::class,
    // ...
];

should be able to fix this problem

Menu