Laravel 5.2 CORS, GET not working with preflight OPTIONS Laravel 5.2 CORS, GET not working with preflight OPTIONS vue.js vue.js

Laravel 5.2 CORS, GET not working with preflight OPTIONS


Clearly not the ideal solution but it WORKS. I've added this to the top of my routes.php file:

header('Access-Control-Allow-Origin: *');header( 'Access-Control-Allow-Headers: Authorization, Content-Type' );

It would be nice to get this working without a hack... alas.

UPDATE: It turned out to be IIS related. I ended up setting the headers in the web.config file and now CORS works without hacking the routes.php file.

<httpProtocol>    <customHeaders>       <add name="Access-Control-Allow-Headers" value="Origin, Authorization, X-Requested-With, Content-Type, Accept" />       <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS,PUT,DELETE" />    </customHeaders></httpProtocol>

If you want to restrict access, you can add outbound rules:

      <outboundRules>          <clear />                <rule name="AddCrossDomainHeader">                    <match serverVariable="RESPONSE_Access_Control_Allow_Origin" pattern=".*" />                    <conditions logicalGrouping="MatchAll" trackAllCaptures="true">                        <add input="{HTTP_ORIGIN}" pattern="(http(s)?://((.+\.)?somesite\.com|(.+\.)?anothersite\.org))" />                    </conditions>                    <action type="Rewrite" value="{C:0}" />                </rule>      </outboundRules>


I solve my problem just adding these line on my routes.php Laravel 5.2For greater then 5.2 in routes/web.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');

OR register Cors middleware in global HTTP middleware stack

protected $middleware = [    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,    \App\Http\Middleware\CorsMiddleware::class];


Laravel 7 or lower:

Your middleware is ok but you need to register Cors middleware in global HTTP middleware stack.

protected $middleware = [    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,    \App\Http\Middleware\CorsMiddleware::class];

Laravel 8 or higher:

All CORS settings may be configured in your application's config/cors.php configuration file. The OPTIONS requests will automatically be handled by the HandleCors middleware that is included by default in your global middleware stack.

Link to the official documentation