Aleksey Dorogov freelance software engineer
← All writing Laravel / PHP

How to fix CORS issues with Chrome localhost Laravel REST API

Aug 2025 1 min read Aleksey Dorogov

Hey, there are a couple issues if you’re working on SPA or simple project where you need to get data from the backend in your JS code, but there is an issue when you try to test it. You can experience the issue with CORS

Origin http://localhost is not allowed by Access-Control-Allow-Origin

If you work with Laravel as a backend and have a REST API - you’re welcome, I will show you the simple way how to fix it for localhost test case.

Ok, everything that you need to do is…put this header to your group of routes, eg.

Route::group(['prefix' => 'api/v1', 'middleware' => []], function () {
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: *");

    /* Products */
    Route::get('products', 'Api\ProductsController@getProducts');
});

That’s it!

Sitting on a version of this problem right now? I'd rather look at it than guess.

Email me
Site down? Message me now Live chat · usually reply in minutes