码迷,mamicode.com
首页 > 其他好文 > 详细

laravel 跨域问题

时间:2019-12-18 15:02:21      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:The   class   lse   exp   sop   get   methods   options   res   

   $response = $next($request);
        $IlluminateResponse = ‘Illuminate\Http\Response‘;
        $SymfonyResopnse = ‘Symfony\Component\HttpFoundation\Response‘;
        $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 \Illuminate\Http\JsonResponse ) {
            Log::info("response {$request->url()}:", $response->getData(true));
        } else{
            Log::info("request {$request->url()}:", $request->all());
        }

  

laravel 跨域问题

标签:The   class   lse   exp   sop   get   methods   options   res   

原文地址:https://www.cnblogs.com/liyanxi/p/12059578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!