主页

在安装完laravel跨域组件后,报错:

prod.ERROR: Call to undefined method Symfony\Component\HttpFoundation\Response::header() {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to undefined method Symfony\\Component\\HttpFoundation\\Response::header() at /data/www/apis.huatu.com/app/Http/Middleware/EnableCrossRequestMiddleware.php:21)
[stacktrace]

解决办法如下:
1、打开/app/Http/Middleware/EnableCrossRequestMiddleware.php
2、找到handle方法
3、将handle内容替换如下

        $response= $next($request);
        $IlluminateResponse= 'Illuminate\Http\Response';
        $SymfonyResopnse= 'Symfony\Component\HttpFoundation\Response';
        $origin= $request->server('HTTP_ORIGIN')? $request->server('HTTP_ORIGIN'): '*';
        $headers= [
            'Access-Control-Allow-Origin' => $origin,
            'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, OPTIONS, DELETE',
            'Access-Control-Allow-Headers' => 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN, User-Agent, Host, Accept-Encoding, Connection, Content-Length',
            'Access-Control-Expose-Headers'=>'Authorization',
            'Access-Control-Allow-Credentials'=>'true',
            'Access-Control-Max-Age'=>'3600'
        ];

        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);
            }
            return $response;
        }
        return $response;

    }

文件参考:https://www.jianshu.com/p/22a82f8dd414

版权属于:三分快乐,七纷幸福
作品采用:本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
1
查看目录

目录

来自 《laravel 跨域报错 Call to undefined method Symfony\Component\HttpFoundation\Response::header()》
评论

三分快乐,七纷幸福
104 文章数
7 评论量
11 分类数
107 页面数
已在风雨中度过 2年246天19小时11分