用于静态资产的Laravel CORS

eeq64g8w  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(135)

我正在用ffmpeg和laravel制作一个视频播放列表,但是当我想从公共存储中获取一个播放列表作为静态资产时,它显示Access-Control-Allow-Origin错误。我尝试了很多方法,但都没有解决。
我用过这个:

namespace App\Http\Middleware;

use Closure;

class CorsMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $headers = [
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Methods'     => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Max-Age'           => '86400',
            'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With'
        ];

        if ($request->isMethod('OPTIONS'))
        {
            return response()->json('{"method":"OPTIONS"}', 200, $headers);
        }

        $response = $next($request);
        foreach($headers as $key => $value)
        {
            $response->header($key, $value);
        }

        return $response;
    }
}

.htaccess中也是如此:

Header set Access-Control-Allow-Origin "*"
also this 
<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

等等

8ehkhllq

8ehkhllq1#

Laravel从9.2版本开始已经实现了https://github.com/fruitcake/laravel-cors。为什么要设置定制中间件?请将您的Laravel安装版本更新到9.2或更高版本,并按照本网站上的步骤配置config/cors.php https://github.com/fruitcake/laravel-cors

相关问题