Vuejs and Laravel Post Request CORS

后端 未结 2 1756
孤独总比滥情好
孤独总比滥情好 2021-01-12 01:17

I dont get it. I am struggling with this since hours

I am using Vue.js with Laravel and try to make a POST Request to an external API.

But i am always getti

2条回答
  •  星月不相逢
    2021-01-12 01:52

    You need to set up the CORS headers from the middleware. Maybe you need some extra setup?

    Anyway, you can create your own middleware and set up the CORS headers in the handle() method like the following example:

    public function handle($request, Closure $next) 
    {
        return $next($request)
               ->header('Access-Control-Allow-Origin', 'http://yourfrontenddomain.com') // maybe put this into the .env file so you can change the URL in production.
               ->header('Access-Control-Allow-Methods', '*') // or specify `'GET, POST, PUT, DELETE'` etc as the second parameter if you want to restrict the methods that are allowed.
               ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization') // or add your headers.
    }
    

    Add your custom middleware to the global $middleware array (under CheckForMaintenanceMode::class) in the Kernel.php class and you should be good to go.

提交回复
热议问题