Preflight OPTIONS request by AngularJS not working with Chrome?

前端 未结 5 985
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 14:52

I have developed a simple application using AngularJS hosted here. I am consuming an API I developed myself in Laravel hosted here. When I try to log into the application us

5条回答
  •  日久生厌
    2021-01-18 15:31

    in filters.php (Laravel 4.2)

    add below

    App::before(function($request)
    {
        // Enable CORS 
        // In production, replace * with http://yourdomain.com 
        header("Access-Control-Allow-Origin: *");
        //header('Access-Control-Allow-Credentials: true'); optional
    
        if (Request::getMethod() == "OPTIONS") {
            // The client-side application can set only headers allowed in Access-Control-Allow-Headers
            $headers = [
                'Access-Control-Allow-Methods'=> '*',
                'Access-Control-Allow-Headers'=> '*'
            ];
            return Response::make('You are connected to the API', 200, $headers);
        }
    });
    

    you can change the values of Access-Control-* to suite your need

提交回复
热议问题