How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen

Have you ever encountered a “No ‘Access-Control-Allow-Origin’ Header” error when accessing an external API server? Probably most of us have experienced it.

The following tutorial I will share how to resolve the No ‘Access-Control-Allow-Origin’ Header error in creating the Restful API Lumen application.

According to the mozilla developer site:

The Access-Control-Allow-Origin response header indicates whether the response can be shared with resources with the given origin.

The point is that a web page can request to send and receive data from the remote API server, but is limited by the same origin policy. If you want to communicate with a remote server outside of the origin, must get permission first by the API server.

Unlike the case, if you use postman they are not limited by this policy.

Usually, when we access an API server that does not set access-control-allow-origin properly it will get an error “Response to the preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:4200’ is therefore not allowed access. The response had HTTP status code 405.”

To overcome the above on PHP server, you can add PHP header as follows:

How To Resolve No Access Control Allow Origin In Lumen 5.5 For Beginners Min

How to resolve No ‘Access-Control-Allow-Origin’ in Lumen 5.5 for beginners

Using Lumen framework You can create your own class to handle Access-Control-Allow-Origin errors. Please create a new class in the app/Http/Middleware folder named CorsMiddleware.php and add the following code

Then add the following code in the app file in the bootstrap/app.php folder

Please try again is there any No ‘Access-Control-Allow-Origin’ Header error at your Restful API Server?

So my brief tutorial on How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen 5.5 for beginners.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.