- Forums
- react
- Allow Cross Site CORS in Laravel/React API - Cross-Origin Request Blocked
This is a simple tutorial that shows you how to enable CORS in your Laravel API Application. [5095], Last Updated: Mon Jun 24, 2024
dd
Sat Mar 11, 2023
0 Comments
3134 Visits
MAKE SURE SERVER IS RUNNING!!
Try it! http://127.0.0.1:8000
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://secured.example.com/api/login (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 204.
Quick Check Cors.php Configs:
'paths' => ['*'], // NOT FOR PRODUCTION
...
'supports_credentials' => true,
Are you seeing this error for your Laravel API? If so, its very simple to enable.
Possible Solutions:
- Check the endpoints: for example: make sure the front end is matching the api.php routes: localhost/api/login vs localhost/login are not the same
- Endpoints - Check the endpoints exists in your api.php routes
- https with https is not allowed unless its the same domain (i think)
- The server is not running or unreachable. Try accessing the URL endpoint directly from the browser
- Edit Config > cors.php - See steps below for cors.php
- Check both frontend and backend match https or http
- There might be an error in the code. Check the PHP Error Logs
For my example, I was running a react application with HTTPS from a localhost to test my app and i kept getting cross origin CORS errors, this is how I fixed it.
To Allow laravel cross domain access to your API follow these steps:
1. Open file: Config > cors.php
Add your host in 'allowed_origins' - for example: https://127.0.0.1:3000
'allowed_origins' => ['https://127.0.0.1:3000'],
2zKoS8GsKK8