Laravel Cors (Middleware NOT working)

后端 未结 8 1552
孤独总比滥情好
孤独总比滥情好 2020-12-31 12:37

I recently tries enabling CORS in Laravel 5.4 but unfortunately it doesn\'t want to work. I have included the code and the error that it\'s giving me below. Can anyone help

8条回答
  •  一向
    一向 (楼主)
    2020-12-31 12:55

    I had a problem handling files using the withHeaders() method, so thanks to the tips below i came up with this working code:

    /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            if ($request->isMethod('OPTIONS'))
            {
                return response()->json('{"method":"OPTIONS"}', 200, $headers);
            }
    
            $response = $next($request);
            $response->headers->set('Access-Control-Expose-Headers', 'Content-Disposition');
            $response->headers->set('Access-Control-Allow-Origin', 'http://localhost:8080','http://localhost','https://edu.pilateswien.org');
            $response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
    
            //return $response->withHeaders($headers);
            return $response;
        }
    

提交回复
热议问题