laravel : Access to XMLHttpRequest at 'http://127.0.0.1:8000/videos' from origin…
- User
laravel : Access to XMLHttpRequest at 'http://127.0.0.1:8000/videos' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Experiences.jsx:20 Error fetching data: AxiosError {message: 'Network Error', name: 'AxiosError', code: 'ERR_NETWORK', config: {…}, request: XMLHttpRequest, …} fetchData @ Experiences.jsx:20 await in fetchData (anonyme) @ Experiences.jsx:24 commitHookEffectListMount @ chunk-6BKLQ22S.js?v=8bbb9753:16915 commitPassiveMountOnFiber @ chunk-6BKLQ22S.js?v=8bbb9753:18156 commitPassiveMountEffects_complete @ chunk-6BKLQ22S.js?v=8bbb9753:18129 commitPassiveMountEffects_begin @ chunk-6BKLQ22S.js?v=8bbb9753:18119 commitPassiveMountEffects @ chunk-6BKLQ22S.js?v=8bbb9753:18109 flushPassiveEffectsImpl @ chunk-6BKLQ22S.js?v=8bbb9753:19490 flushPassiveEffects @ chunk-6BKLQ22S.js?v=8bbb9753:19447 (anonyme) @ chunk-6BKLQ22S.js?v=8bbb9753:19328 workLoop @ chunk-6BKLQ22S.js?v=8bbb9753:197 flushWork @ chunk-6BKLQ22S.js?v=8bbb9753:176 performWorkUntilDeadline @ chunk-6BKLQ22S.js?v=8bbb9753:384 Afficher 11 autres frames Afficher moins Experiences.jsx:13
GET http://127.0.0.1:8000/videos net::ERR_FAILED 200 (OK)dispatchXhrRequest @ axios.js?t=1729549753671&v=2dc2a8f4:1680 xhr @ axios.js?t=1729549753671&v=2dc2a8f4:1560 dispatchRequest @ axios.js?t=1729549753671&v=2dc2a8f4:2035 _request @ axios.js?t=1729549753671&v=2dc2a8f4:2240 request @ axios.js?t=1729549753671&v=2dc2a8f4:2141 Axios. @ axios.js?t=1729549753671&v=2dc2a8f4:2259 wrap @ axios.js?t=1729549753671&v=2dc2a8f4:8 fetchData @ Experiences.jsx:13 (anonyme) @ Experiences.jsx:24 commitHookEffectListMount @ chunk-6BKLQ22S.js?v=8bbb9753:16915 commitPassiveMountOnFiber @ chunk-6BKLQ22S.js?v=8bbb9753:18156 commitPassiveMountEffects_complete @ chunk-6BKLQ22S.js?v=8bbb9753:18129 commitPassiveMountEffects_begin @ chunk-6BKLQ22S.js?v=8bbb9753:18119 commitPassiveMountEffects @ chunk-6BKLQ22S.js?v=8bbb9753:18109 flushPassiveEffectsImpl @ chunk-6BKLQ22S.js?v=8bbb9753:19490 flushPassiveEffects @ chunk-6BKLQ22S.js?v=8bbb9753:19447 (anonyme) @ chunk-6BKLQ22S.js?v=8bbb9753:19328 workLoop @ chunk-6BKLQ22S.js?v=8bbb9753:197 flushWork @ chunk-6BKLQ22S.js?v=8bbb9753:176 performWorkUntilDeadline @ chunk-6BKLQ22S.js?v=8bbb9753:384 Afficher 18 autres frames Afficher moins localhost/:1 Access to XMLHttpRequest at 'http://127.0.0.1:8000/videos' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Experiences.jsx:20 Error fetching data: AxiosError {message: 'Network Error', name: 'AxiosError', code: 'ERR_NETWORK', config: {…}, request: XMLHttpRequest, …} fetchData @ Experiences.jsx:20 await in fetchData (anonyme) @ Experiences.jsx:24 commitHookEffectListMount @ chunk-6BKLQ22S.js?v=8bbb9753:16915 invokePassiveEffectMountInDEV @ chunk-6BKLQ22S.js?v=8bbb9753:18324 invokeEffectsInDev @ chunk-6BKLQ22S.js?v=8bbb9753:19701 commitDoubleInvokeEffectsInDEV @ chunk-6BKLQ22S.js?v=8bbb9753:19686 flushPassiveEffectsImpl @ chunk-6BKLQ22S.js?v=8bbb9753:19503 flushPassiveEffects @ chunk-6BKLQ22S.js?v=8bbb9753:19447 (anonyme) @ chunk-6BKLQ22S.js?v=8bbb9753:19328 workLoop @ chunk-6BKLQ22S.js?v=8bbb9753:197 flushWork @ chunk-6BKLQ22S.js?v=8bbb9753:176 performWorkUntilDeadline @ chunk-6BKLQ22S.js?v=8bbb9753:384 Afficher 10 autres frames Afficher moins Experiences.jsx:13
GET http://127.0.0.1:8000/videos net::ERR_FAILED 200 (OK)dispatchXhrRequest @ axios.js?t=1729549753671&v=2dc2a8f4:1680 xhr @ axios.js?t=1729549753671&v=2dc2a8f4:1560 dispatchRequest @ axios.js?t=1729549753671&v=2dc2a8f4:2035 _request @ axios.js?t=1729549753671&v=2dc2a8f4:2240 request @ axios.js?t=1729549753671&v=2dc2a8f4:2141 Axios. @ axios.js?t=1729549753671&v=2dc2a8f4:2259 wrap @ axios.js?t=1729549753671&v=2dc2a8f4:8 fetchData @ Experiences.jsx:13 (anonyme) @ Experiences.jsx:24 commitHookEffectListMount @ chunk-6BKLQ22S.js?v=8bbb9753:16915 invokePassiveEffectMountInDEV @ chunk-6BKLQ22S.js?v=8bbb9753:18324 invokeEffectsInDev @ chunk-6BKLQ22S.js?v=8bbb9753:19701 commitDoubleInvokeEffectsInDEV @ chunk-6BKLQ22S.js?v=8bbb9753:19686 flushPassiveEffectsImpl @ chunk-6BKLQ22S.js?v=8bbb9753:19503 flushPassiveEffects @ chunk-6BKLQ22S.js?v=8bbb9753:19447 (anonyme) @ chunk-6BKLQ22S.js?v=8bbb9753:19328 workLoop @ chunk-6BKLQ22S.js?v=8bbb9753:197 flushWork @ chunk-6BKLQ22S.js?v=8bbb9753:176 performWorkUntilDeadline @ chunk-6BKLQ22S.js?v=8bbb9753:384 Afficher 17 autres frames Afficher moins
- Assistant
The error you're encountering is a CORS (Cross-Origin Resource Sharing) issue. This typically occurs when your frontend application (running on
http://localhost:5173) tries to make a request to your Laravel backend (http://127.0.0.1:8000/videos) but the backend isn't configured to accept requests from that origin.Here's a step-by-step guide to resolve this issue:
1. Configure CORS in Laravel
Laravel provides built-in support for handling CORS. You need to ensure that it's properly configured to allow requests from your frontend origin.
a. Locate the CORS Configuration File
Find the
cors.phpconfiguration file, which is usually located atconfig/cors.php. If it doesn’t exist, you might need to install the FruitCake Laravel CORS package, although it's included by default in recent Laravel versions.b. Update CORS Settings
Modify the
cors.phpfile to allow requests from your frontend origin. Here's an example configuration:return [ /* |-------------------------------------------------------------------------- | Cross-Origin Resource Sharing (CORS) Configuration |-------------------------------------------------------------------------- */ 'paths' => ['api/*', 'videos'], // Add 'videos' or any other endpoints you need 'allowed_methods' => ['*'], // Allows all HTTP methods (GET, POST, etc.) 'allowed_origins' => ['http://localhost:5173'], // Frontend origin 'allowed_origins_patterns' => [], 'allowed_headers' => ['*'], // Allows all headers 'exposed_headers' => [], 'max_age' => 0, 'supports_credentials' => false, ];Explanation of Settings:
-
paths: Specifies the endpoints where CORS headers should be applied. Make sure to include all relevant routes, such as'videos'in your case. -
allowed_methods: Defines which HTTP methods are allowed. Using['*']permits all methods. -
allowed_origins: Lists the origins that are allowed to make cross-origin requests. Replace'http://localhost:5173'with your frontend's origin. -
allowed_headers: Specifies which headers are allowed in the requests. Using['*']permits all headers. -
supports_credentials: Determines if credentials like cookies or HTTP authentication are allowed. Set totrueif needed.
c. Ensure CORS Middleware is Enabled
Check the
app/Http/Kernel.phpfile to ensure that the CORS middleware is registered. It should be included in the global middleware stack:protected $middleware = [ // ... \Fruitcake\Cors\HandleCors::class, // ... ];If it’s not present, add the above line to include the CORS middleware.
d. Clear Configuration Cache
After making changes to the configuration, clear the cache to ensure Laravel picks up the new settings:
php artisan config:clear php artisan cache:clearAlternatively, you can run:
php artisan config:cache2. Verify the Changes
After updating the CORS settings:
-
Restart Your Laravel Server: If you're running
php artisan serve, stop and restart it to apply the changes. -
Test the API Endpoint: Try accessing
http://127.0.0.1:8000/videosfrom your frontend application. The CORS error should be resolved.
3. Additional Tips
-
Development vs. Production: It's common to have more relaxed CORS settings in development. However, ensure that in production, you restrict
allowed_originsto trusted domains to enhance security. -
Preflight Requests: Browsers may send a preflight
OPTIONSrequest to check permissions. Ensure your server correctly handles these requests. Laravel's CORS middleware typically manages this automatically. -
Using Wildcards: While using
['*']forallowed_originsis convenient during development, it's not recommended for production as it allows any domain to access your API.
4. Alternative: Using a Proxy
If configuring CORS feels cumbersome, especially during development, you can set up a proxy in your frontend development server to route API requests. This approach makes the browser think the requests are coming from the same origin.
For example, if you're using Vite (common with Vue.js or React):
- Update
vite.config.js:
export default { // ... other configurations server: { proxy: { '/videos': 'http://127.0.0.1:8000', }, }, };- Use Relative URLs in Axios:
Instead of using the full URL, use relative paths:
axios.get('/videos') .then(response => { // handle success }) .catch(error => { // handle error });This setup proxies the
/videosrequests to your Laravel backend, bypassing CORS restrictions.5. Final Checks
-
Backend Response: Ensure your Laravel endpoints are correctly returning responses. A CORS error might sometimes mask other underlying issues like server errors.
-
Browser Cache: Sometimes, browsers cache CORS preflight responses. Clear your browser cache or try in an incognito/private window.
Summary
By properly configuring the CORS settings in your Laravel backend to allow requests from your frontend's origin (
http://localhost:5173), you should be able to eliminate the CORS policy error. Ensure that these settings are secure, especially when moving to a production environment.If you continue to face issues after following these steps, please provide additional details about your Laravel and frontend setup, and any other error messages you might be encountering.
-