laravel 跨域问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel 跨域问题相关的知识,希望对你有一定的参考价值。

   $response = $next($request);
        $IlluminateResponse = ‘IlluminateHttpResponse‘;
        $SymfonyResopnse = ‘SymfonyComponentHttpFoundationResponse‘;
        $headers = [
            ‘Access-Control-Allow-Origin‘ => ‘*‘,
            ‘Access-Control-Allow-Methods‘ => ‘POST, GET, OPTIONS, PUT, PATCH, DELETE‘,
            ‘Access-Control-Allow-Headers‘ => ‘Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers, X-CSRF-TOKEN‘
        ];
        
        if ($response instanceof $IlluminateResponse) {
            foreach ($headers as $key => $value) {
                $response->header($key, $value);
            }
            return $response;
        }

        if ($response instanceof $SymfonyResopnse) {
            foreach ($headers as $key => $value) {
                $response->headers->set($key, $value);
            }
//            Log::info("request {$request->url()}:", $request->all());
            return $response;
        }

        return $response;

  

 $response = $next($request);
        $origin = $request->server(‘HTTP_ORIGIN‘) ? $request->server(‘HTTP_ORIGIN‘) : ‘‘;
        $response->header(‘Access-Control-Allow-Origin‘, $origin);
        $response->header(‘Access-Control-Allow-Headers‘, ‘Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN‘);
        $response->header(‘Access-Control-Expose-Headers‘, ‘Authorization, authenticated‘);
        $response->header(‘Access-Control-Allow-Methods‘, ‘GET, POST, PATCH, PUT, OPTIONS‘);
        $response->header(‘Access-Control-Allow-Credentials‘, ‘true‘);
        if ($response instanceof IlluminateHttpJsonResponse ) {
            Log::info("response {$request->url()}:", $response->getData(true));
        } else{
            Log::info("request {$request->url()}:", $request->all());
        }

  

以上是关于laravel 跨域问题的主要内容,如果未能解决你的问题,请参考以下文章

跨域图片上传Angular+laravel

需要一种有效的方法来避免使用 Laravel 5 重复代码片段

laravel特殊功能代码片段集合

laravel 接口跨域

Laravel:如何在控制器的几种方法中重用代码片段

laravel 跨域问题