I was getting a CORS error when calling an endpoint, see error below:
XMLHttpRequest cannot load http://domain.com/v1/airtime. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://domain.com' is therefore not allowed access. The response had HTTP status code 404.
I added Header set Access-Control-Allow-Origin "*" in the .htaccess file in the API root folder. The error disappeared and was replaced with the one below:
XMLHttpRequest cannot load domain/v1/airtime. Response for preflight has invalid HTTP status code 404
Every other solutions I sought could not resolve the issue. What can I do to handle the preflight error?
Below is the code which I used to solve CORS in the BaseController but seems it is not enough and I am still getting preflight error.
// add CORS filter $behaviors['corsFilter'] = [ 'class' => Cors::className(), 'cors' => [ // restrict access to 'Origin' => ['*'], 'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'], // Allow only POST and PUT methods 'Access-Control-Request-Headers' => ['*'], // Allow only headers 'X-Wsse' 'Access-Control-Allow-Credentials' => true, // Allow OPTIONS caching 'Access-Control-Max-Age' => 86400, // Allow the X-Pagination-Current-Page header to be exposed to the browser. 'Access-Control-Expose-Headers' => [], ];` My proxy.conf.json file has the content below:
{ "/api": { "target": "http://domain.com/v1", "secure": false, "pathRewrite" : {"^api" : ""} } }
Access-Control-Allow-Originto*on your server side (yii), or use a proxy in your angular2 application. stackoverflow.com/questions/36002493/…