Preflight OPTIONS request by AngularJS not working with Chrome?

前端 未结 5 946
爱一瞬间的悲伤
爱一瞬间的悲伤 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:48

    In Laravel, try to set : 'Access-Control-Allow-Methods' & 'Access-Control-Allow-Headers' to '*'

    public function handle($request, Closure $next)
     { 
    
       header("Access-Control-Allow-Origin: *");
    
      // ALLOW OPTIONS METHOD
      $headers = [
             'Access-Control-Allow-Methods'=> '*',
             'Access-Control-Allow-Headers'=> '*'
         ];
      if($request->getMethod() == "OPTIONS") {
             // The client-side application can set only headers allowed in Access-Control-Allow-Headers
             return Response::make('OK', 200, $headers);
         }
    
         $response = $next($request);
         foreach($headers as $key => $value) 
          $response->header($key, $value);
      return $response;
     }
    

提交回复
热议问题