Vuejs and Laravel Post Request CORS Vuejs and Laravel Post Request CORS laravel laravel

Vuejs and Laravel Post Request CORS


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.


Other way (without creating a new laravel middleware) is add these headers at the begining of your routes.php

header('Access-Control-Allow-Origin:  *');header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, DELETE');header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Origin, Authorization');

and add this before your interceptors on vue:Vue.http.options.crossOrigin = true