I have been connecting my Laravel API backend to my NextJS frontend. Everything worked well until I put part of my NextJS app onto a subdomain and I immediately got CORS errors.
"Access to XMLHttpRequest at 'http://myapp.test:8080/dashboard' (redirected from 'http://myapp.test:8080/api/login') from origin 'http://app.myapp.test:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
http://myapp.test:8080/api/login receives data back, but when I set it to it’s normally (working when on same domain) behaviour of redirecting to the main application, it throws the error.
Any help would be appreciated.
2
Answers
Hi have you tried the following method
//AppHttpMiddleware;
check config/cors.php
change paths array to * (‘paths’ => [‘*’])
i hope it solves the issue
As google says:
Read more
An example of a cross-origin request, JavaScript code served from
https://domain-a.com
uses ajax to make a request forhttps://domain-b.com/data.json
Now browser will block that request due to security.
Solution
The solution is to send a header for some domains. An example of nginx:
More secure option:
In laravel you can manually send headers or use middlewares