Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
I’m building an application with Vue (VueX, vue-router, …) and OctoberCMS. I create virtual domain for API and APP in local via MAMP. I had config all CORS via plugin ‘rluders-cors’ in OctoberCMS.
Route::group(
[
'prefix' => 'api/v1',
'middleware' => ['api','jwt.auth','BarryvdhCorsHandleCors'],
],
function () {
// Routes here
}
);
When I deployed into VPS (Centos 6), I have above response from browser. I had added header in .htaccess file of API. And when I open API dirrectly via browser (turn of jwt.auth middleware), I get all header which set in .htaccess file. But in App, don’t work.
I tried to use primary domain for App (abc.example) to call the API from my virtual domain in local (local.local), it still worked. But I tried local App to call the primary API in VPS, it didn’t work.
Please help me! Thanks.
2
Answers
So my solution for routes was to look for a preflight response. So in the request data if it is empty I am sending the preflight response (I think any string would do). Then here comes a request that is not empty. This was the easiest work around that I have found. You still have to allow access control domain. I typically set it to all and always have a token system to authenticate methods other than GET.
In my case, it worked with :