Response to preflightLaravel – request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’

You will get following errors when you test your REST API application with React, Vue.JS and angular

You will get following errors when you call the REST api

Failed to load http://yourdomain.com/api/somemethod Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:3333’ is therefore not allowed access.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://yourdomain.com/api/somemethod. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

To solve this issue in Laravel you can create middleware

Now you can add following code to App\Http\Middleware\CORSMiddleware

In your App\Http\Kernel.php you can add the following code

Now you can call the REST API from your javascript code without any issue