1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Laravel 11 API suddenly returns CORS Error: No 'Access-Control-Allow-Origin' header is present...

Discussão em 'Outras Linguagens' iniciado por PioPio, Novembro 4, 2024 às 12:12.

  1. PioPio

    PioPio Guest

    I have a backend API written in Laravel, which worked fine until recently. Suddenly when I make a POST request the console shows a CORS error:

    Access to XMLHttpRequest at 'http://localhost:8000/api/comments' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    I've hit this endpoint many times before without issue, so I'm not sure why it would suddenly throw it now.

    cors.php

    return [

    /*
    |--------------------------------------------------------------------------
    | Cross-Origin Resource Sharing (CORS) Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your settings for cross-origin resource sharing
    | or "CORS". This determines what cross-origin operations may execute
    | in web browsers. You are free to adjust these settings as needed.
    |
    | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
    |
    */

    'paths' => ['*'],

    'allowed_methods' => ['*'],

    'allowed_origins' => [env('FRONTEND_URL', '*')],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

    ];


    .env

    FRONTEND_URL=http://localhost:5173

    Continue reading...

Compartilhe esta Página