javascriptphpangularlaravel

Getting CORS error while calling API from Angular 16 to Laravel 10


Getting below error while calling API from Angular to Laravel

Access to XMLHttpRequest at 'http://localhost:8000/api/v1/authenticate' from origin 'http://localhost:4201' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I already tried adding headers in Angular for CORS but nothing worked for me. I have checked Laravel 10 cors file every thing is good from there and I also added below code in boostrap/app.php

header("Access-Control-Allow-Headers: Origin, userKey, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Headers, Authorization, observe, enctype, Content-Length, X-Csrf-Token");
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 3600");
header('content-type: application/json; charset=utf-8');

Solution

  • Try below Suggestion, Add Middle as instructed in Stack Overflow Answer.

    CORS error, unable to solve, Why always getting Access-Control-Allow-Origin?