Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Laravel 8 Error Cors #539

Open
ghost opened this issue Jul 27, 2021 · 2 comments
Open

Laravel 8 Error Cors #539

ghost opened this issue Jul 27, 2021 · 2 comments

Comments

@ghost
Copy link

ghost commented Jul 27, 2021

I have an Api Rest with Laravel 8 and a front-end with VueJS, everything works fine from postman, but when making requests from Vue it gives cors error.

I also tried creating a Cors middleware that contains the following:

    public function handle(Request $request, Closure $next)
    {
        return $next($request)->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods','GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    }

It also keeps throwing cors error, the library that includes the laravel version is: "fruitcake / laravel-cors": "^ 2.0", but this still doesn't work.

How could I fix my cors error?

@insideode
Copy link

I have same problem. If you upgrade Laravel version, then it is over.

@ghost
Copy link
Author

ghost commented Jul 30, 2021

I was able to solve the problem in this way, in my case it was due to a configuration that needed the web.config file, I had to add the following

    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="*" />
        <add name="Access-Control-Allow-Headers" value="Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization" />
      </customHeaders>
    </httpProtocol>

And my file stayed like this

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)/$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php" />
        </rule>
      </rules>
    </rewrite>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="*" />
        <add name="Access-Control-Allow-Headers" value="Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

This alone still gives a Cors error, but it is due to the implementation of the fruitcake that the laravel version already brings, it is only necessary to comment that class in the kernel

    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        // \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class
    ];

Now with this it works fine

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant