Report this

What is the reason for this report?

Allow CORS for webroot files!

Posted on July 23, 2020

I need to fetch and download files from webroot/datafiles/images/filename.jpg, but I got CORS error!

The download works when the link is opened through navigator (not fetch http)

ERROR Access to XMLHttpRequest at ‘http://159.89.93.8/datafiles/arquivos/01dadd40-cd06-11ea-bf2e-7384e360a612.jpeg?ignoreInterceptor=true’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

export function download(file: File) {
  return api.get(normalizeFileUrl(file.name), {
    responseType: 'blob',
  }).then(response => {
    FileDownload(response, `${file.nome}`);
  });
}

I’m using cakephp as API it’s already configured CORS!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

The CORS error occurs when the server does not include the appropriate CORS headers in the response, or when the headers do not allow access from the specified origin.

In your case, it seems that the Access-Control-Allow-Headers value in the server response does not include the access-control-allow-origin header. To resolve this issue, you need to update your CakePHP configuration to include the correct CORS headers.

Update the CakePHP CORS configuration: Locate your config/app.php or config/app_local.php file, and look for the Cors middleware configuration. Update it to include the necessary headers:

'Cors' => [
    'allowOrigin' => ['http://localhost:8080'], // or use '*' to allow any origin
    'allowMethods' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
    'allowHeaders' => ['Content-Type', 'Origin', 'X-Requested-With', 'Accept', 'Authorization', 'Access-Control-Allow-Origin'],
    'exposeHeaders' => ['Link'],
    'maxAge' => 300,
    'supportsCredentials' => true,
],

Load the CorsMiddleware: In your src/Application.php file, make sure you have the following code to load the CorsMiddleware:

use Cake\Http\Middleware\CorsMiddleware;

// ...

public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{
    // ...

    $cors = new CorsMiddleware((array)$this->getConfig('Cors'));
    $middlewareQueue->add($cors);

    return $middlewareQueue;
}

Clear the CakePHP cache: Run the following command to clear the CakePHP cache:

  1. bin/cake cache clear_all

After making these updates, your CakePHP application should include the correct CORS headers in the response, allowing requests from http://localhost:8080. If you still encounter issues, double-check the configuration and make sure there are no typos or missing settings.

Keep in mind that using '*' to allow any origin can expose your API to security risks. Use it with caution, and only for testing purposes. In production, replace it with the specific origin(s) you want to allow.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.