[Fixed] CORS Issue: No 'Access-Control-Allow-Origin' Header Present on Laravel 10 API Request – Laravel

by
Alexei Petrov
axios composer-php cors laravel

Quick Fix: Create a middleware to set the ‘Access-Control-Allow-Origin’ header in the API response. Define this middleware in the ‘app/Http/Kernel.php’ file and apply it to the API routes using ‘Route::middleware()’.

The Problem:

Despite configuring CORS on the Laravel API to allow all origins and methods, a React app is facing CORS issues when making requests to the API. The error message ‘No ‘Access-Control-Allow-Origin’ header is present on the requested resource’ is displayed in the console. The issue persists even though the server is returning the expected response when accessed directly through Postman. What could be causing this problem, and what additional steps should be taken to ensure successful requests from the React app to the Laravel API?

The Solutions:

Solution 2: Install Allow CORS: Access-Control-Allow-Origin Chrome Extension

The CORS issue can be resolved by employing the Allow CORS: Access-Control-Allow-Origin extension in the Google Chrome browser. This extension facilitates the addition of CORS headers to the responses received from web servers, thereby alleviating the issue of CORS restrictions.

To effectively use this extension, follow these steps:

  1. Install the Extension: Visit the Chrome Web Store and search for the Allow CORS: Access-Control-Allow-Origin extension. Install it like any other Chrome extension by clicking the Add to Chrome button.

  2. Configure the Extension: Once installed, click on the extension icon in the toolbar. Select the Options tab and ensure that the Enabled checkbox is ticked.

  3. Customize CORS Headers: In the Custom CORS Headers section, you can define the desired CORS headers to be added to the responses. By default, the extension adds the **Access-Control-Allow-Origin: *** header, which allows requests from any origin. If you want to limit access to specific allowed URLs, you can configure that here.

  4. Enable for Specific Websites: To enable CORS for a specific website, click on the extension icon and select the Enabled for this site checkbox. Alternatively, you can set the extension to be active for all websites by clicking the checkbox next to Enabled Everywhere.

  5. Reload the Page: Once you have completed the configuration, refresh the page where you were experiencing the CORS issue. The extension will automatically add the necessary CORS headers to the responses.

Q&A

What could be causing this CORS issue despite having the correct CORS configuration on my Laravel API?

There might be a configuration issue in the .env file or an issue with the middleware or the route group.

What is one possible solution to resolve the CORS issue in a Laravel API?

One solution is to create a middleware to handle CORS requests and add it to the route group or individual routes that need CORS support.

What is the purpose of running the php artisan config:clear command in this context?

Running php artisan config:clear command ensures that any cached configuration is cleared, ensuring that the latest configuration is being used.

Video Explanation:

The following video, titled "”How", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

”Fix

| React Tutorial. Hong Ly Tech•222K views · 6 … Why I No longer Hate CORS and Why you should too! CoderOne•15K views.”]