Laravel 9 - CORS is not working (Access to XMLHttpRequest has been blocked by CORS policy)

Laravel 9 - CORS is not working (Access to XMLHttpRequest has been blocked by CORS policy)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving CORS Blocking in Laravel 9 and Vue.js Applications

When building a separated frontend application using Vue.js that fetches data from a Laravel 9 API, you may encounter the following error in the browser console:

Access to XMLHttpRequest at 'http://localhost:8000/api' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This error occurs because of Cross-Origin Resource Sharing (CORS) security mechanisms enforced by web browsers. The browser blocks requests from a different origin (e.g., http://127.0.0.1:8080) to a server running on another domain or port (e.g., http://localhost:8000) unless the server explicitly allows it via specific HTTP headers.

In your Laravel 9 setup, you have configured the CORS middleware in config/cors.php with 'allowed_origins' => ['*']. However, this configuration alone might not be sufficient if the middleware is not correctly applied or if there are issues with how the response is being generated. Additionally, relying on browser extensions to bypass CORS (like adding Access-Control-Allow-Origin headers via a Google extension) is not a production-ready solution because it only works in development environments and does not fix the underlying issue.

Configuring CORS Properly in Laravel 9

To resolve this issue, ensure that the CORS middleware is properly registered in your Laravel application. In Laravel 9, you can define custom CORS policies or use the default configuration depending on your needs. If you are using allowed_origins as an array of strings, make sure it includes both http://localhost:8000 and http://127.0.0.1:8080, since these are technically different origins in the eyes of the browser.

If you want to allow all origins (for development only), use 'allowed_origins' => ['*']. For production, it's better to specify exact domains or use patterns like 'https://*.example.com'.

You should also verify that the CORS middleware is included in your route group definition. In your api.php routes file, ensure that the routes are defined within a middleware group that includes the CORS middleware:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('/api', [UserController::class, 'index']);

And in your RouteServiceProvider, register the CORS middleware for API routes:

public function boot()
{
    $this->registerPolicies();

    Route::middleware(['cors'])->group(function () {
        // Your API routes here
    });
}

Alternatively, you can apply the CORS middleware directly in your route definitions or use Laravel's CorsServiceProvider to configure global CORS settings.

Handling Preflight Requests

Another common cause of CORS issues is not handling preflight requests (OPTIONS). Laravel's default CORS configuration should handle these automatically if the 'allowed_methods' and 'max_age' are set correctly. However, ensure that your server returns appropriate headers for both simple and preflight requests.

Check your response headers using browser developer tools to confirm that Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers are present. If they are missing, it indicates that the CORS middleware is either not applied or misconfigured.

Avoiding Common Pitfalls

One mistake developers often make is assuming that setting 'allowed_origins' => ['*'] will always work. While this may appear to solve the issue in local development, it can pose a security risk in production environments where sensitive data might be exposed. Always restrict CORS policies to trusted origins when deploying to production.

Another pitfall is relying on browser extensions to bypass CORS errors. These tools inject headers into responses client-side but do not address the server-side configuration required for production use. Solving CORS issues at the server level ensures that your application behaves correctly across all environments.

Final Considerations

If you're still encountering issues after applying these configurations, consider checking your Laravel version and ensuring compatibility with the latest updates. You may also want to review any custom middleware or route filters that could be interfering with CORS headers.

For more in-depth guidance on setting up secure APIs with Laravel, resources like https://laravelcompany.com can offer additional insights tailored to modern Laravel versions.

Our network