2

We are using the vue-bulma-admin scaffold on a project, which already uses webpack, and we can't seem to perform any POST/PUT requests to our API (which already sets access-cross-allow-origin headers to *), only GET requests works. From different front-ends that consumes our API, we don't have this problem, only with this specific front-end, which gives as this common error:

"No 'Access-Control-Allow-Origin' header is present on the requested resource."

Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested 
resource.

We already tried to set custom headers on axios/vue-resource, but with no success. As I said, using the same routes through Postman / different front-ends or with the CORS chrome extension, everything works fine.

Any ideas to work around this?

My Middleware Cors in Laravel API.

<?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)
          ->header('Access-Control-Allow-Origin', '*')
          ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}
  • Do you also set Access-Control-Allow-Methods? – Nora Sep 05 '17 at 15:48
  • I edited my question with it. – Bruno Justino Praciano Sep 05 '17 at 15:50
  • It sounds like your POST is adding at least one header (probably the Content-Type header?), which triggers your browser to, automatically on its own, do a CORS preflight OPTIONS request https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests And it appears the backend code of that server isn’t configured to send the Access-Control-Allow-Origin header for OPTIONS requests. It must be configured to respond to OPTIONS requests with a 200 OK and the Access-Control-Allow-Headers and Access-Control-Allow-Methods headers with the right values. – sideshowbarker Sep 05 '17 at 20:45
  • See https://stackoverflow.com/questions/8719276/cors-with-php-headers/9866124#9866124 for some example code that handles the CORS preflight OPTIONS – sideshowbarker Sep 05 '17 at 20:53

1 Answers1

0

I resolved my problem using a package https://github.com/barryvdh/laravel-cors