independent full stack developer

/ Developer's blog

How to fix CORS issues with Chrome localhost Laravel REST API

Thursday, June 20, 2024

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!


3793 people already saw it