Menu

Fix Common CORS Errors in APIs and Frontend Apps
🖼️

Fix Common CORS Errors in APIs and Frontend Apps

DEV Community·Rafli Zocky·6 months ago
#FRRbcu8I
#issue#webdev#api#javascript#enter
Reading 0:00
15s threshold

I've spent many hours solving this…

Cross-Origin Resource Sharing (CORS) errors are a regular part of web development. More info: MDN CORS Guide .

Issue 1: No Access-Control-Allow-Origin Header

Access to XMLHttpRequest blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Enter fullscreen mode

Exit fullscreen mode

The frontend is calling a backend endpoint without proper CORS configuration. Fix:

'allowed_origins' => [ 'http://localhost:3000' , 'http://localhost:5173' , ],

Enter fullscreen mode

Exit fullscreen mode

For Laravel 11+:

php artisan config:publish cors

Enter fullscreen mode

Exit fullscreen mode

Then edit config/cors.php .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.