Allow Cross Origin in Laravel 4.2

Discussão em 'Angular' iniciado por Ish, Abril 4, 2025.

  1. Ish

    Ish Guest

    I am working on a legacy project which uses Laravel 4.2 for the backend API and angular for the frontend. When I make the login request in local pc I am seeing CORS error in developer tool network tab.

    Laravel API runnin on http://localhost:8000/ and angular front end running on http://0.0.0.0:4200/

    To allow cross origin support from my laravel backend API I added below code to my app > start > global.php

    But still CORS issue is there. I am looking for some help to solve this issue. Thanks

    App::before(function($request)
    {
    // Get the origin of the request
    $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';

    // Set CORS headers for all requests
    header('Access-Control-Allow-Origin: ' . $origin);
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // Cache preflight response for 1 day

    // If the request is an OPTIONS method, handle it separately
    if ($request->getMethod() == 'OPTIONS') {
    // Terminate preflight request here
    header('HTTP/1.1 200 OK');
    exit;
    }

    });

    Continue reading...

Compartilhe esta Página