Angular 2 - No 'Access-Control-Allow-Origin' header is present on the requested resource Angular 2 - No 'Access-Control-Allow-Origin' header is present on the requested resource laravel laravel

Angular 2 - No 'Access-Control-Allow-Origin' header is present on the requested resource


Ok. seem like need to configure apache for it.

i'm using xampp webserver, and I had to edit my httpd.conf as explained here to solve this.

Added this line:

Header set Access-Control-Allow-Origin "http://localhost:3000"

solved my problem.

Restarting apache is neccessary.


Open chrome inspect tool, switch to Network tab and inspect the request Angular2 sent.

In Headers->Response Headers, check whether there is Access-Control-Allow-Origin:* (I bet not)

If you are building an API, the easiest workaround is to add

if (Request::is("api/*")) {    header("Access-Control-Allow-Origin: *");    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, If-Modified-Since, Cache-Control, Pragma");}

in the beginning of routes.php, using a Middleware would be a better approach, but make sure it is working properly and adding the Access-Control-Allow-Origin to the response header.


//Add this middleware in your express appapp.use(function (req, res, next) {res.setHeader('Access-Control-Allow-Headers', 'Content-Type');res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');res.header('access-Control-Allow-Origin', '*');next();});